Python,使用 "if, elif" 时出现语法错误

Python, syntax error using "if, elif"

我是 Python 的新手,在尝试编写一个脚本时会不断询问有关用户的问题,直到脚本变为 FALSE,

我决定检查一下纸条,当然它给了我一个语法错误,告诉我错误在第五道,`a.

现在在那条车道上,我试图将 a 的旧值更改为新值。 可悲的是,我无法理解我犯的错误,有人可以检查一下并解释一下哪里出了问题吗?

    #!/usr/bin/python

print "Hello, I'm wilfred and I'm an Artificial Intelligence\n"

a=str(raw_input("Do you want to be my friend? \n"))

if a=="yes":
a=str(raw_input("Yey ! my first friend,what is your name?\n"))               
    if a==str :
        print "Nice name man!"
    elif a==int :
        print "bye!"
elif a=="no":
    print "Well, nice to meet you anway, good bye now \n"

你的线路

a=str(raw_input("Yey ! my first friend,what is your name?\n")  
  • 缩进此行,使其位于 'if' 语句内
  • 在此行末尾添加一个')'

这种重复循环的一般结构是

while True:
    a=str(raw_input(...)) 
    if a=="whatever": break
    # other responses to a

您只需缩进该行即可。您的代码应该可以正常工作。继续学习python。这很棒!!!!

#!/usr/bin/python

print "Hello, I'm wilfred and I'm an Artificial Intelligence\n"

a=str(raw_input("Do you want to be my friend? \n"))

if a=="yes":
    a=str(raw_input("Yey ! my first friend,what is your name?\n"))               
    if a==str :
        print "Nice name man!"
    elif a==int :
        print "bye!"
elif a=="no":
    print "Well, nice to meet you anway, good bye now \n"

为了进一步帮助测试用例,我为您更改了字符串和整数测试。 “==”测试是为了顺便说一句。

#!/usr/bin/python

print "Hello, I'm wilfred and I'm an Artificial Intelligence\n"

a=str(raw_input("Do you want to be my friend? \n"))

if a=="yes":
    a=str(raw_input("Yey ! my first friend,what is your name?\n"))
    if a.isalpha() :
        print "Nice name man!"
    elif a.isdigit() :
        print "bye!"
elif a=="no":
    print "Well, nice to meet you anway, good bye now \n"