基本的python这个脚本有什么问题

时间:2017-03-10 14:54:41

标签: python-3.x

我开始学习Python。我写了这个剧本但是当我输入Kevin和0时,它显示“Hello world”而不是“Kevin is great”。

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")

2 个答案:

答案 0 :(得分:0)

input()函数返回一个字符串(在这种情况下,当你输入0时它将返回“0”)所以你可以用int()函数将它解析成一个字符串,如下所示:

print ("hello world") 
myName = input("what is your name?") 
myVar = input("Enter a number: ")

myVar = int(myVar)
if (myName == "Kevin" and myVar == 0):
    print ("Kevin is great!") 
elif (myName == "Bob"): 
    print("You are not great") 
else: 
    print("Hello world")

有关详细信息,请查看文档:

Input() Documentation

Int() Documentation

答案 1 :(得分:0)

您的第二个变量将提示用户输入字符串而不是整数。您需要添加“int(”或“float”(在“input”之前“)以将字符串更改为整数。例如:

print ("hello world") myName = input("what is your name?") myVar = int(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") code here