[英]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.