【发布时间】:2021-04-20 05:49:36
【问题描述】:
我试图提示用户输入一个 int 或一个字符串。 int 很好用,但是每当我尝试输入一个字符串时,它都会给我错误“ValueError: invalid literal for int() with base 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))
【问题讨论】:
标签: python if-statement input