简体   繁体   English

为什么我在使用 if 语句时不断收到语法错误?

[英]Why do I keep getting a Syntax error while working with if statements?

country = input("Please enter a country: ")

if country == United Kingdom:
    print("Accents in the United Kingdom change noticeably about every 40km.")

elif country == Italy:
    print("Italy has three active volcanoes: Vesuvius, Etna, and Stromboli.")

elif country == France:
    print("France is the world's most popular tourist destination.")

elif country == Germany:
    print("One-third of Germany is still covered in forests and woodlands.")

elif country == Japan:
    print("In Japanese, the name Japan is Nihon or Nippon, which means Land of the Rising Sun.")

elif country == USA:
    print("100 acres of pizza are served in the United States every day!")

elif country == Canada:
    print("The Royal Montreal Golf Club is the oldest golf club in North America.")

else: 
    print(country + " is currently not a G7 country")

I keep getting a syntax error but I can not seem to find my error.我不断收到语法错误,但似乎找不到我的错误。 My error is: Traceback (most recent call last): File "python", line 3 if country == United Kingdom: ^ SyntaxError: invalid syntax我的错误是: Traceback(最近一次调用最后一次):文件“python”,如果国家/地区 == 英国,第 3 行:^ SyntaxError:无效语法

You need quotes around your string values.您需要在字符串值周围加上引号。

if country == 'United Kingdom':

the problem after each "==", the syntax is not correct, try adding quotes to each country每个“==”后面的问题,语法不正确,试试给每个国家加引号

country = str(input("Please enter a country: "))

if country == "United Kingdom":
    print("Accents in the United Kingdom change noticeably about every 40km.")

elif country == "Italy":
    print("Italy has three active volcanoes: Vesuvius, Etna, and Stromboli.")

elif country == "France":
    print("France is the world's most popular tourist destination.")

elif country == "Germany":
    print("One-third of Germany is still covered in forests and woodlands.")

elif country == "Japan":
    print("In Japanese, the name Japan is Nihon or Nippon, which means Land of the Rising Sun.")

elif country == "USA":
    print("100 acres of pizza are served in the United States every day!")

elif country == "Canada":
    print("The Royal Montreal Golf Club is the oldest golf club in North America.")

else: 
    print(country + " is currently not a G7 country")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM