我需要接受用户的电话号码并打印总和。我希望定义的对象接受用户输入的字符仅为整数类型。
输入值后,我不想检查对象的类型。我想要一种方法,程序甚至不接受字符串或其他数据类型,而仅接受整数。
答案 0 :(得分:0)
我非常确定您可以使用try
/ except
并将用户的输入转换为int
:
# Repeat until a "break" is issued
while True:
number = input('Enter number: ')
# Attempt to convert the input to an integer
try:
number = int(number)
# If there was an error converting...
except ValueError:
print("That's not a number! Try again.")
# Break the loop if no error, i.e. conversion was successful
else:
break