小能豆

基本的Python计算器

python

我是Python新手。我尝试制作一个简单的计算器,但问题出在哪里?

def add(num1, num2):
    return num1 + num2

def subtract(num1, num2):
    return num1 - num2

def div(num1, num2):
    return num1/num2

def multi(num1,num2):
    return num1*num2

def main():
    operation = input("What do you want to do?(+, -, *, or /):")
    if (operation != "+" and operation != "-" and operation != "*" and operation != "/"):
        print("Your input is invalid. Please enter a valid input.")
    else:
        num1 = float(input("Enter value for num1: "))
        num2 = float(input("Enter value for num2: "))
        if (operation == "+"):
            print(add(num1, num2))
        elif (operation == "-"):
            print(subtract(num1, num2))
        elif (operation == "*"):
            print(multi(num1,num2))
        elif (operation == "/"):
            print(div(num1,num2))

    main()

阅读 123

收藏
2023-09-09

共1个答案

小能豆

你的代码基本上是正确的,但有一个小问题导致了无限递归调用 main() 函数,这会导致程序不停地询问用户输入操作,而不会退出。为了解决这个问题,你需要将 main() 函数的调用移到代码的外部,以便程序在完成一次计算后退出。

这是修复后的代码:

``你的代码基本上是正确的,但有一个小问题导致了无限递归调用main()函数,这会导致程序不停地询问用户输入操作,而不会退出。为了解决这个问题,你需要将main()` 函数的调用移到代码的外部,以便程序在完成一次计算后退出。

这是修复后的代码:

def add(num1, num2):
    return num1 + num2

def subtract(num1, num2):
    return num1 - num2

def div(num1, num2):
    return num1 / num2

def multi(num1, num2):
    return num1 * num2

def main():
    operation = input("What do you want to do? (+, -, *, or /): ")
    if operation not in ["+", "-", "*", "/"]:
        print("Your input is invalid. Please enter a valid input.")
    else:
        num1 = float(input("Enter value for num1: "))
        num2 = float(input("Enter value for num2: "))
        if operation == "+":
            print(add(num1, num2))
        elif operation == "-":
            print(subtract(num1, num2))
        elif operation == "*":
            print(multi(num1, num2))
        elif operation == "/":
            if num2 == 0:
                print("Division by zero is not allowed.")
            else:
                print(div(num1, num2))

if __name__ == "__main__":
    main()

重要的变化包括将 main() 函数的调用移到 if __name__ == "__main__": 块中,这样程序只有在直接运行时才会执行 main(),而不会无限递归调用。此外,我还添加了对除法中除数为零的检查。
def add(num1, num2):
return num1 + num2

def subtract(num1, num2):
return num1 - num2

def div(num1, num2):
return num1 / num2

def multi(num1, num2):
return num1 * num2

def main():
operation = input(“What do you want to do? (+, -, , or /): “)
if operation not in [“+”, “-“, “
”, “/”]:
print(“Your input is invalid. Please enter a valid input.”)
else:
num1 = float(input(“Enter value for num1: “))
num2 = float(input(“Enter value for num2: “))
if operation == “+”:
print(add(num1, num2))
elif operation == “-“:
print(subtract(num1, num2))
elif operation == “*”:
print(multi(num1, num2))
elif operation == “/”:
if num2 == 0:
print(“Division by zero is not allowed.”)
else:
print(div(num1, num2))

if name == “main”:
main()
```

重要的变化包括将 main() 函数的调用移到 if __name__ == "__main__": 块中,这样程序只有在直接运行时才会执行 main(),而不会无限递归调用。此外,我还添加了对除法中除数为零的检查。

2023-09-09