[英]SyntaxError invalid syntax python
def convert():
choice = input("Wybierz kierunek wymiany waluty \n1) PLN>USD \n2) USD>PLN \n3) PLN>EURO \n4) EURO>PLN")
print(choice)
if choice == 1:
print ("choice=1")
else choice == 2:
print("Choice=2")
else choice == 3:
print("Choice=3")
else choice == 4:
print("Choice=4")
convert()
为什么会有SyntaxError: invalid syntax
?
您可能指的是elif
- python 中没有else <cond>
:
def convert():
choice = input("Wybierz kierunek wymiany waluty \n1) PLN>USD \n2) USD>PLN \n3) PLN>EURO \n4) EURO>PLN")
print(choice)
if choice == "1":
print("choice=1")
elif choice == "2":
print("Choice=2")
elif choice == "3":
print("Choice=3")
elif choice == "4":
print("Choice=4")
convert()
请参阅文档以了解如何使用 if-elif-else。
python 中有这个三重奏: if
、 elif
和else
。 只能有一个else
,因为,只要想想它的实际含义:只有在所有其他条件都没有通过时才做某事。
def convert():
choice = input("Wybierz kierunek wymiany waluty \n1) PLN>USD \n2) USD>PLN \n3) PLN>EURO \n4) EURO>PLN")
print(choice)
if choice == 1:
print ("choice=1")
elif choice == 2:
print("Choice=2")
elif choice == 3:
print("Choice=3")
elif choice == 4:
print("Choice=4")
convert()
虽然修复代码的传统方法是用elif
替换除最后一个之外的所有else
,如下所示:
def convert():
choice = input("Wybierz kierunek wymiany waluty \n1) PLN>USD \n2) USD>PLN \n3) PLN>EURO \n4) EURO>PLN")
print(choice)
if choice == "1":
print("choice=1")
elif choice == "2":
print("Choice=2")
elif choice == "3":
print("Choice=3")
elif choice == "4":
print("Choice=4")
convert()
但是,对于您的特定情况,即使不需要 else 也是如此。 您可以 go 为:
def convert():
choice = input("Wybierz kierunek wymiany waluty \n1) PLN>USD \n2) USD>PLN \n3) PLN>EURO \n4) EURO>PLN")
print(choice)
print("choice=", choice)
convert()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.