簡體   English   中英

從while循環返回“無”

[英]“None” as return from while loop

我有以下功能:

def AdjustTime(f):
    if len(f) == 1:
        return '0' + f + '00'
    elif len(f) == 2:
        return f + '00'
    elif len(f) == 3:
        return '0' + f
    elif len(f) == 4:
        return f
    else:
        while True:
            if len(f) > 0 and len(f) <= 4 and int(f[:2]) <= 23 and int(f[2:]) <= 59:
                return f
                break
            else:
                clear()
                print f,'Get this date right'
                f = raw_input('')

它一直有效,直到我得到正確的數字,從而導致TypeError:'NoneType'對象不可下標。 如何解決這個問題?

編輯:首先,感謝括號中的提及,我在編寫自己的代碼時忘記了幾次,現在的代碼是我實際上正在嘗試的代碼。

我想將Drafts帶來的文本字符串放入此函數中,if / elif會將1-2-3字符串轉換為我需要的4位數字以及我想要的方式。 例如,字符串“ 1”將變為“ 0100”。 但是你知道的。 如果用戶以某種方式搞砸了,我會在一段時間內使用它。 是的,我應該以其他方式重新組織它,例如在實際嘗試編輯字符串之前,使用int(f[:2]) <= 23 and int(f[2:]) <= 59

回到正軌,如果用戶搞砸了,輸入將使他有機會插入正確的字符串,該字符串會經過一會兒。 問題是,當用戶輸入正確的值時,這是print f顯示的,將值視為1234:

1234
None

現在,我還能做些什么來幫助您?

EDIT2:由於每個人都在要求完整的代碼,因此您在這里是為了幫助我,我只是認為沒有必要。 抱歉(:

from urllib import quote
import time
from webbrowser import open
from console import clear

rgv = ['a path', 'This is an awesome reminder\nWith\nMultiple\nLines.\nThe last line will be the time\n23455']

a = rgv[1].split('\n')

reminder = quote('\n'.join(a[:(len(a)-1)]))

t = a[len(a)-1]

def AdjustTime(f):
    if len(f) == 1:
    return '0' + f + '00'
    elif len(f) == 2:
        return f + '00'
    elif len(f) == 3:
        return '0' + f
    elif len(f) == 4:
        return f
    else:
        while True:
            if len(f) > 0 and len(f) <= 4 and int(f[:2]) <= 23 and int(f[2:]) <= 59:
                return f
                break
            else:
                clear()
                print 'Get this date right'
                f = raw_input('')

mins = int(AdjustTime(t)[:2])*60 + int(AdjustTime(t)[2:])

local = (time.localtime().tm_hour*60+time.localtime().tm_min)

def findTime():
    if local < mins:
        return mins - local
    else: 
        return mins - local + 1440

due = 'due://x-callback-url/add?title=' + reminder + '&minslater=' + str(findTime()) + '&x-source=Drafts&x-success=drafts://'

open(due)
def AdjustTime(f):
    f = f or ""   # in case None was passed in
    while True:
        f = f.zfill(4)
        if f.isdigit() and len(f) == 4 and int(f[:2]) <= 23 and int(f[2:]) <= 59:
            return f
        clear()
        print f, 'Get this date right'
        f = raw_input('')

您需要初始化f才能說"" while True的第一個迭代中while True f為None ,因此在if條件下,它正在測試None[:2]None[2:] ,這顯然會引發錯誤。

編輯:好吧,我想知道為什么你不明白

object of type 'NoneType' has no len()

錯誤第一...

在方法頂部,添加以下內容:

def AdjustTime(f):
   if not f:
      return

如果傳遞了“ falsey”值,這將阻止該方法執行。

為了做到這一點,您需要更改邏輯,以在該函數的調用raw_input包含raw_input行。 因為上面的方法將返回並且提示將永遠不會顯示:

def AdjustTime(f):
    if not f:
       return
    if len(f) == 1:
        return '0' + f + '00'
    if len(f) == 2:
        return f + '00'
    if len(f) == 3:
        return '0' + f
    if len(f) == 4:
        return f
    if 0 > len(f) <= 4 and int(f[:2]) <= 23 and int(f[2:] <= 59:
        return f

def get_input():
    f = raw_input('')
    result = AdjustTime(f)
    while not result:
        print('{} get this date right'.format(f))
        f = raw_input('')
        result = AdjustTime(f)

@gnibbler在評論中有一個很好的建議:

def AdjustTime(f):
   f = f or ""

如果傳入的值是falsey,這會將f的值設置為空白字符串。 這種方法的好處是您的if循環仍將運行(因為空白字符串具有長度),但是while循環將失敗。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM