【问题标题】:input() typing issue [duplicate]输入()输入问题[重复]
【发布时间】:2022-01-02 05:30:57
【问题描述】:
x = True
y = False
i = input("How many nose you have : ")

ans = 1

if i == 1:
    print(x)
elif i == 2:
    print(y)
print(ans)

如果我输入 2,它应该打印出 y 的值。但是,它不会为此输入打印 y 的值。我不知道如何解决这个问题。

【问题讨论】:

    标签: python if-statement


    【解决方案1】:

    input() 返回一个字符串。你应该使用:

    i = int(input("How many nose you have : "))
    

    将输入转换为整数。

    【讨论】:

      【解决方案2】:

      python 的 input() 以字符串形式获取所有内容,要在其他数据类型中使用它,您必须对其进行类型转换或在字符串中进行比较

      x = True
      y = False
      i = input("How many nose you have : ")
      
      ans = 1
      
      if int(i)==1:
          print(x)
      elif int(i)==2:
          print(y)
      print(ans)
      

      【讨论】:

      • x = True y = False i = int(input("How many nose you have : "))ans = str(1)if i == 1: print(x) else: print(False) print("The answer is " + ans)
      • 我是这样做的
      【解决方案3】:
      x = True
      y = True
      i = int(input("How many nose you have: "))
      
      ans = 1
      
      if i == 1:
          print(x)
      elif i == 2:
          print(y)
      print(ans)
      

      虽然我不建议这样做,因为你仍然可以做 3 和其他数字。我建议这样做:

      a = int(input("How many nose do you have?: "))
      ans = 1
      
      if a == ans:
          print(True)
      else:
          print(False)
      print("The answer is " + ans + "!")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-05
        • 1970-01-01
        • 1970-01-01
        • 2012-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多