Python语法无效,不断出错

2024-04-16 19:11:45 发布

您现在位置: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()

Tags: andofthe代码inputreturnifdef
1条回答
网友
1楼 · 发布于 2024-04-16 19:11:45

以下几行缺少右括号:

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

因此,在num2行上会出现语法错误,因为Python从未找到从前一行开始的逻辑行的结尾。你知道吗

平衡括号:

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

否则你的代码就行了。你知道吗

相关问题 更多 >