【问题标题】:code works without the input at the beginning, why?代码在开头没有输入的情况下工作,为什么?
【发布时间】:2019-02-27 13:38:24
【问题描述】:

如果我一开始就删除了spam = input(),那么当我手动为spam 赋值时,代码就会起作用。但是,如下所示,当我被提示为spam 赋值时,无论我给出什么,它都会告诉我“您好!”为什么?

spam = input()
if spam == 1:
    print('Hello')
elif spam == 2:
    print('Howdy')
else:
    print('Greetings!')

【问题讨论】:

    标签: python if-statement input


    【解决方案1】:

    input 返回一个字符串,但您将它与整数文字(12)进行比较。一种选择是改用字符串文字:

    if spam == '1': # Here
        print('Hello')
    elif spam == '2': # And here
        print('Howdy')
    else:
        print('Greetings!') 
    

    【讨论】:

      猜你喜欢
      • 2015-09-06
      • 2015-09-17
      • 2013-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-19
      • 1970-01-01
      相关资源
      最近更新 更多