[英]Why do I keep getting TypeError even though input is an interger?
I'm coding something for a school project which asks the user for a number between 0-10 and I have no idea why I keep getting a TypeError even though the input is converted to integer using a list.我正在为一个学校项目编写一些代码,该项目要求用户输入一个介于 0-10 之间的数字,我不知道为什么即使使用列表将输入转换为 integer,我仍然会收到 TypeError。 Any help please?
请问有什么帮助吗? I'm doing this on Wing IDE 101.
我在 Wing IDE 101 上执行此操作。
numbers = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
while True:
num = ''
while num == '':
try:
num = input("Please enter a number: ")
num = int(num)
except ValueError:
if num in numbers:
num = numbers.index(num)
num = int(num)
else:
print('Invalid input, please enter valid input')
if num == 0:
print("Thank you for using this program")
break
elif num < 0 or num > 10:
print("This is an invalid number, please enter a valid number.")
print()
continue
I think this the reason:我觉得是这个原因:
When your input is not a valid input like "asdf"
.当您的输入不是像
"asdf"
这样的有效输入时。 It will jump out the inner while loop.它会跳出内部的while循环。 Because you inner while loop condition is
num == ''
while num
is "asdf"
now.因为你内部的 while 循环条件是
num == ''
而num
现在是"asdf"
。
You should just put num = ""
under print('Invalid input, please enter valid input')
and your code will work and not raise TypeError anymore.您只需将
num = ""
放在print('Invalid input, please enter valid input')
下,您的代码就可以正常工作并且不再引发 TypeError。
Complete code below:完整代码如下:
numbers = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
while True:
num = ''
while num == '':
try:
num = input("Please enter a number: ")
num = int(num)
except ValueError:
if num in numbers:
num = numbers.index(num)
num = int(num)
else:
print('Invalid input, please enter valid input')
num = ""
if num == 0:
print("Thank you for using this program")
break
elif num < 0 or num > 10:
print("This is an invalid number, please enter a valid number.")
print()
continue
In fact your code could be simpfy to one loop:实际上,您的代码可以简化为一个循环:
numbers = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
while True:
num = input("Please enter a number: ")
if num in numbers:
num = numbers.index(num)
else:
try:
num = int(num)
except ValueError:
print("Invalid input, please enter valid input")
continue
if num == 0:
print("Thank you for using this program")
break
elif num < 0 or num > 10:
print("This is an invalid number, please enter a valid number.")
What happens to you can happen a lot in dynamic language since the types are resolved on runtime rather on design time, so sometimes it is hard to figure.发生在你身上的事情在动态语言中可能会发生很多,因为类型是在运行时而不是在设计时解析的,所以有时很难确定。
First, separate the user_input
from the number ( num
) you are evaluating, into 2 different parameters (one for user input string and one for integer) - I know it does not matter on Python, but it is a good practice.首先,将
user_input
与您正在评估的数字 ( num
) 分离为 2 个不同的参数(一个用于用户输入字符串,一个用于整数) - 我知道在 Python 上并不重要,但这是一个很好的做法。
Now your num
value will only be assigned on a successful parsing.现在,您的
num
值只会在成功解析时分配。 No need for continue
at the end of the loop as well在循环结束时也不需要
continue
Code Snippet代码片段
numbers = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
while True:
user_input = ''
num = None # default invalid value
while num is None:
try:
user_input = input("Please enter a number: ")
num = int(user_input)
except ValueError:
if user_input in numbers:
num = numbers.index(user_input)
else:
print('Invalid input, please enter valid input')
print()
if num == 0:
print("Thank you for using this program")
break
elif num < 0 or num > 10:
print("This is an invalid number, please enter a valid number.")
print()
Debug Output调试Output
Please enter a number: 1
Please enter a number: one
Please enter a number: 11
This is an invalid number, please enter a valid number.
Please enter a number: -1
This is an invalid number, please enter a valid number.
Please enter a number: no numbder
Invalid input, please enter valid input
Please enter a number: 0
Thank you for using this program
Process finished with exit code 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.