【问题标题】:If input == list如果输入 == 列表
【发布时间】:2022-01-20 10:05:30
【问题描述】:

所以基本上我试图做到这一点,如果用户输入是 ==“蓝色”或“蓝色”,它会给 eye_res 的值 1。如果不是,那么给它 0 的值。我试过的是:

Eye_color = input("Eye color: ")
A= (int["Blue","blue"])

if Eye_color in range (str(A)):
    eye_res = 1
else:
    eye_res = 0

print (eye_res)

【问题讨论】:

  • 你在用A= (int["Blue","blue"])做什么?
  • print(int(input("Eye color: ").strip().lower() == "blue"))
  • 我强烈建议您重新阅读您的学习材料或一些好的教程,这比尝试或多或少的随机代码组合更有效。

标签: python python-3.x list


【解决方案1】:

我相信这可以满足您的需求:

Eye_color = input("Eye color: ")

if Eye_color in ["Blue", "blue"]:
    eye_res = 1
else:
    eye_res = 0

print(eye_res)

如果你想先保存列表,你也可以这样做:

Eye_color = input("Eye color: ")
A = ["Blue", "blue"]

if Eye_color in A:
    eye_res = 1
else:
    eye_res = 0

print(eye_res)

另外,如果您希望他们能够以任何方式将“蓝色”大写并且eye_res 仍然是 1,那么您可以这样做:

Eye_color = input("Eye color: ")

if Eye_color.lower() == 'blue':
    eye_res = 1
else:
    eye_res = 0

print(eye_res)

【讨论】:

    【解决方案2】:

    您可以通过在输入上使用修饰符来与设定值进行比较。

    Eye_color = input("Eye color: ")
    
    if Eye_color.lower() == “blue”:
        eye_res = 1
    else:
        eye_res = 0
    
    print(eye_res)
    

    这使得“blue”的任何大小写都将eye_res 设置为1,同时将Eye_color 变量保持为精确的输入字符串。

    【讨论】:

      【解决方案3】:

      我不明白你在A= (int["Blue","blue"]) 中的逻辑是什么 这是您想要实现的代码:

      Eye_color = input("Eye color : ")
      A = ["Blue", "blue"]
      if Eye_color in A:
          eye_res = 1
      else:
          eye_res = 0
      print(eye_res)
      

      这也是单线解决方案:

      print(1 if input("Eye color : ").lower() == "blue" else 0)
      

      【讨论】:

        【解决方案4】:

        你可以这样做:

        eye_color = input("Eye color: ").lower()
        
        eye_res = int(eye_color == 'blue')
        

        【讨论】:

          【解决方案5】:

          您可以将答案浓缩为:

          eye_res = int(input("Eye color: ") in ["Blue", "blue"])
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-11-21
            • 1970-01-01
            • 2020-02-05
            • 2021-12-03
            • 2011-12-28
            • 1970-01-01
            相关资源
            最近更新 更多