python易犯错误之put()

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

    print('yes')

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

因为spam=‘1’

1!=‘1’

改正后

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

    print('yes')

这样就能运行正常了

猜你喜欢

转载自blog.csdn.net/a66666_/article/details/80952335
今日推荐