我在这段python代码中找不到我做错了什么

时间:2017-10-13 09:32:25

标签: python

当我尝试运行这段代码时:

capitals = ["Lisbon", "Madrid", "Paris", "Berlin", "London", 
"Reykjavik", "Dublin", "Stockolm", "Rome"]
client_name = "Me"
while True:
answer = input(client_name + ": ")
if answer == "What is the capital of Germany?"
print(capitals[3])
else:
print("I don't understand!")

它给了我这个错误:

File "chatbot.py", line 4
answer = input(client_name + ": ")
     ^

但是我不明白我做错了什么,有人可以帮助我吗?

2 个答案:

答案 0 :(得分:-1)

缩进和遗失":"在if结束时:

capitals = ["Lisbon", "Madrid", "Paris", "Berlin", "London", 
"Reykjavik", "Dublin", "Stockolm", "Rome"]
client_name = "Me"
while True:
    answer = input(client_name + ": ")
    if answer == "What is the capital of Germany?":
        print(capitals[3])
    else:  
        print("I don't understand!")

答案 1 :(得分:-1)

将您的代码更改为:

capitals = ["Lisbon", "Madrid", "Paris", "Berlin", "London", 
            "Reykjavik", "Dublin", "Stockolm", "Rome"]
client_name = "Me"
while True:
    answer = input(client_name + ": ")
    if answer == "What is the capital of Germany?":
        print(capitals[3])
    else:
        print("I don't understand!")

python中的缩进是代码的一部分而且你缺少":"在if语句之后