简体   繁体   English

SyntaxError 语法无效 python

[英]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()

Why is there a SyntaxError: invalid syntax ?为什么会有SyntaxError: invalid syntax

You probably meant elif - there is no else <cond> in python:您可能指的是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()

See the docs for how to use if-elif-else.请参阅文档以了解如何使用 if-elif-else。

There is this trio in python: if , elif , and else . python 中有这个三重奏: ifelifelse There can only be one else , because, just think of what it actually means: only do something when all other conditions don't pass.只能有一个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()

While traditional way to fix your code is by replacing all else except the last one with elif as follows:虽然修复代码的传统方法是用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()

However for your particular case, even if else isn't needed.但是,对于您的特定情况,即使不需要 else 也是如此。 You can go for:您可以 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.

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