【问题标题】:I keep getting an ValueError: invalid literal for int() with base 10: 'red'我不断收到 ValueError: invalid literal for int() with base 10: 'red'
【发布时间】: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


    【解决方案1】:

    将“红色”转换为整数完全没有意义。所以用户应该不能输入自己的颜色或输入颜色。他们必须输入一个整数,但是没有必要输入一个字符串。

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

    【讨论】:

      【解决方案2】:

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


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

      所以在您的代码中运行的第一件事是:

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

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

      int(num1) == 1

      但如果num1 是一个字符串(如“green”),则int(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!')
      
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 2018-02-01
          • 2020-08-02
          • 1970-01-01
          • 1970-01-01
          • 2018-05-07
          • 2011-02-06
          • 2021-12-25
          • 2022-01-09
          • 2019-04-16
          相关资源
          最近更新 更多