我试图提示用户输入整数或字符串。 int 效果很好,但是每当我尝试输入字符串时,它都会给我错误“ValueError:int() 的无效文字,基数为 10:'green'”。我试过寻找答案,但没有运气。任何帮助将不胜感激。谢谢。
One = "1 = Red"
Two = "2 = Blue"
Three = "3 = Yellow"
Four = "4 = Pink"
Five = "5 = Purple"
#Prints listed colors
print ("*List of Colors below*")
print (One + "\n" + Two + "\n" + Three + "\n" + Four + "\n" + Five)
num1 = str(input('What is your favorite color? You can either choose the number from the list above or enter your own color: '))
if (int(num1) == 1):
print ('You chose the the first color from the list, which is: Red')
elif (int(num1) == 2):
print ('You chose the the first color from the list, which is: Blue')
elif (int(num1) == 3):
print ('You chose the the first color from the list, which is: Yellow')
elif (int(num1) == 4):
print ('You chose the the first color from the list, which is: Pink')
elif (int(num1) == 5):
print ('You chose the the first color from the list, which is: Purple')
elif (int(num1) > 6):
print ('!Please choose numbers between 1-5!')
else:
print ("You have entered your own color: " + str(num1))
答案 0 :(得分:1)
将“红色”变成整数完全没有意义。所以用户不应该能够输入他们自己的颜色或输入颜色。他们必须输入一个整数,但这样就没有必要输入字符串了。
num1 = int(input('What is your favorite color? Choose a number from above: '))
答案 1 :(得分:1)
这是因为不能将不是 int 的字符串转换为 int
If 子句首先计算括号中的条件,然后在计算结果为 True
时执行嵌套代码。
所以在你的代码中运行的第一件事是:
if (int(num1) == 1): ...
因此,要评估该语句的奇偶校验,它必须执行:
int(num1) == 1
但是如果 int(num1)
是一个字符串(如“green”),则 num1
不是未定义的。因此代码会产生错误。您需要做的是首先检查值 num1
是否为字符串,然后将其转换为数字后使用您的案例。
在python中有很多方法可以检查这是否是整数,可以考虑看看这里:https://www.pythonpool.com/python-check-if-string-is-integer/
最终代码可能类似于:
One = "1 = Red"
Two = "2 = Blue"
Three = "3 = Yellow"
Four = "4 = Pink"
Five = "5 = Purple"
#Prints listed colors
print ("*List of Colors below*")
print (One + "\n" + Two + "\n" + Three + "\n" + Four + "\n" + Five)
num1 = input('What is your favorite color? You can either choose the number from the list above or enter your own color: ')
if !num1.isnumeric():
print ("You have entered your own color: " + str(num1))
elif (int(num1) == 1):
print ('You chose the the first color from the list, which is: Red')
elif (int(num1) == 2):
print ('You chose the the first color from the list, which is: Blue')
elif (int(num1) == 3):
print ('You chose the the first color from the list, which is: Yellow')
elif (int(num1) == 4):
print ('You chose the the first color from the list, which is: Pink')
elif (int(num1) == 5):
print ('You chose the the first color from the list, which is: Purple')
else (int(num1) > 6):
print ('!Please choose numbers between 1-5!')
答案 2 :(得分:0)
错误是由于尝试将非数字字符串转换为整数。 您应该使用 try/except 构造:
num1 = str(input('What is your favorite color? You can either choose the number from the list above or enter your own color: '))
try:
if (int(num1) == 1):
print ('You chose the the first color from the list, which is: Red')
elif (int(num1) == 2):
print ('You chose the the first color from the list, which is: Blue')
elif (int(num1) == 3):
print ('You chose the the first color from the list, which is: Yellow')
elif (int(num1) == 4):
print ('You chose the the first color from the list, which is: Pink')
elif (int(num1) == 5):
print ('You chose the the first color from the list, which is: Purple')
elif (int(num1) > 6):
print ('!Please choose numbers between 1-5!')
except ValueError:
print ("You have entered your own color: " + str(num1))