【发布时间】:2017-03-11 00:14:36
【问题描述】:
我开始学习 Python。我写了这个脚本,但是当我输入 Kevin 和 0 时,它显示“Hello world”而不是“Kevin 很棒”。
print ("hello world")
myName = input("what is your name?")
myVar = input("Enter a number: ")
if( myName == "Kevin" and myVar == 0):
print ("Kevin is great!")
elif(myName == "Bob"):
print("You are not great")
else:
print("Hello world")
【问题讨论】:
-
您需要先将
myVar转换为int。或者检查myVar == '0'而不是myVar == 0。因为input()返回的是str(字符串)而不是int -
在我看来,首先从 Python 2 开始。此外,出于安全原因,请使用 raw_input 而不是 input。
-
@Ambitions 我不会向刚开始学习 python 的人推荐 python 2。 Python 2 很快就会停止支持,所以无论如何你都必须学习 Python 3。
-
非常感谢@Farhan.K
标签: python-3.x