繁体   English   中英

我不断收到 ValueError: invalid literal for int() with base 10: 'red'

[英]I keep getting an ValueError: invalid literal for int() with base 10: 'red'

我试图提示用户输入一个 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))

将“红色”变成 integer 完全没有意义。 所以用户应该不能输入自己的颜色或输入颜色。 他们必须输入 integer,但是没有必要输入字符串。

num1 = int(input('What is your favorite color? Choose a number from above: '))

这是因为不是 int 的字符串不能转换为int


if 子句首先计算括号中的条件,然后在计算结果为True时执行嵌套代码。

因此,在您的代码中运行的第一件事是:

if (int(num1) == 1): ...

因此,要评估该语句的奇偶性,它必须执行:

int(num1) == 1

但是如果num1是一个字符串(如“green”),则int(num1)不是未定义的。 因此代码会产生错误。 您需要做的是首先检查值num1是否为字符串,然后在将其转换为数字后使用您的案例。

有很多方法可以检查这是否是 python 中的 integer,考虑看看这里: https://www.pythonpool.com/python-check-if-string-is-is-isger/

最终代码可能类似于:

    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!')

该错误是由于尝试将非数字字符串转换为 integer。 您应该使用 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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM