【发布时间】:2012-01-07 20:04:06
【问题描述】:
这是我的代码:
def get_user_list(message, datatype):
while True:
str_input = input(message)
if str_input == '':
print('Error: Received no input.')
for i in range(str_input - 1):
if datatype == 'int':
try:
return int(str_input)
except ValueError:
print("Error: Element '" + str_input[i] + "' is not of type " + datatype + ".")
elif datatype == 'float':
try:
return float(str_input)
except ValueError:
print("Error: Element '" + str_input[i] + "' is not of type " + datatype + ".")
else:
return str_input
我无法弄清楚它有什么问题......请大家帮忙......谢谢
【问题讨论】:
-
你可能被小语言欺骗,认为自动类型转换很好。例如,Perl 和 Javascript 允许
"5" - 1。 Python 没有,因为它实际上是一个非常糟糕的主意。另见stackoverflow.com/a/1995298/126214 -
另外,我不明白 for 循环的用途。
标签: python python-3.x