无效的语法Python,继续出错

时间:2015-05-28 11:54:34

标签: python

所以我一直在努力编写代码。在我遇到这个问题之前,它一直很简单有趣。我想要做的只是一个简单的计算器,但每次我尝试运行程序时都会遇到错误。我已经在论坛上重新检查了它并向许多人寻求帮助,没有运气我没有找到任何正确的答案。所以这里是代码,请告诉我问题。

print("Welcome To Bargi's Calculator")
print("Enjoy")

#Returns the sum of no1 and no2    
def Add(no1, no2):
    return no1 + no2

#Returns the subtraction of no1 and no2
def Subtract(no1, no2):
    return no1 - no2

#Returns the multiplication of no1 and nom2
def Multiply(no1, no2):
    return no1 * no2

#Returns the division of no1 and no
def Divide(no1, no2):
    return no1 / no2

def main():
    operation = input("What do you want to do? (+, -, * or /):")
    if(operation != "+" and operation != "-" and operation != "*" and operation != "/" ):
        #invalid operation
        print("Please Put In A Valid Answer.")
    else:
        num1 = int(input("Enter no1: ")
        num2 = int(input("Enter no2: ")
        if(operation == "+"):
             print(Add(num1, num2))
        if(operation == "-"):
             print(Subtract(num1, num2))
        if(operation == "*"):
            print(Multiply(num1, num2))
        if(operation == "/"):
            print(Divide(num1, num2))

main()

1 个答案:

答案 0 :(得分:3)

您缺少这些行的右括号:

num1 = int(input("Enter no1: ")
num2 = int(input("Enter no2: ")

因此,您会在num2行上收到语法错误,因为Python从未发现逻辑行的结尾在前一行开始。

平衡括号:

num1 = int(input("Enter no1: "))
num2 = int(input("Enter no2: "))

,否则您的代码将起作用。