【发布时间】:2013-09-03 13:06:14
【问题描述】:
我有以下功能:
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' object is not subscriptable。如何解决这个问题?
编辑:首先,感谢括号中的提及,我在自己编码时忘记了几次,现在代码是我实际尝试的代码。
我想把从 Drafts 带来的一串文本放到这个函数中,if/elif 会将一个 1-2-3 字符串转换成我需要的 4 位数字以及我想要的方式。例如,字符串“1”将变为“0100”。但你知道的。如果用户以某种方式搞砸了,我正在使用那段时间。是的,我应该以其他方式重新组织它,例如在实际尝试编辑字符串之前使用int(f[:2]) <= 23 and int(f[2:]) <= 59。
回到正轨,如果用户搞砸了,输入让他有机会插入一个正确的字符串,该字符串通过 while。问题是,当用户输入正确的值时,这就是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)
【问题讨论】:
-
在
return声明之后不需要break。您应该显示示例输入/输出 -
f 是一个非常糟糕的变量名。请阅读 pep8 标准。
-
if 语句中的最后一个
int()缺少) -
您在
if len(f) > 0 ...行中缺少)。所以很明显我们不是在看你实际运行的代码 -
len(f) > 0 and len(f) <= 4最好写成0 < len(f) <=4
标签: python