python 易犯错误之input()

xiaoxiao2021-02-28  25

print('what is your name') spam=input() if spam==1:       print('Hello') elif spam==2:     print('howdy') else:

    print('yes')

在上面这段代码中,即使你输入1,结果也是yes

因为spam=‘1’,input默认的将输入转化为字符串类型,数字1与字符‘1’是不相等的

改正后

print('what is your name') spam=input() if int(spam)==1:     print('Hello') elif int(spam)==2:     print('howdy') else:

    print('yes')

这样就能运行正常了

转载请注明原文地址: https://www.6miu.com/read-2622591.html

最新回复(0)