我有一个简单的Python计算,我需要一个;“退出”;选择

我有一个简单的Python计算,我需要一个;“退出”;选择,python,function,calculator,Python,Function,Calculator,这是我的简单计算和定义函数,我需要一个退出/中断菜单中的选择,我想要一个这样的选择代码 # Program make a simple calculator # This function adds two numbers def add(x, y): return x + y # This function subtracts two numbers def subtract(x, y): return x - y # This function multiplies t

这是我的简单计算和定义函数,我需要一个退出/中断菜单中的选择,我想要一个这样的选择代码

# Program make a simple calculator

# This function adds two numbers
def add(x, y):
    return x + y

# This function subtracts two numbers
def subtract(x, y):
    return x - y

# This function multiplies two numbers
def multiply(x, y):
    return x * y

# This function divides two numbers
def divide(x, y):
    return x / y

print("İşlem seçiniz.")
print("1.Toplama")
print("2.Çıkarma")
print("3.Çarpma")
print("4.Bölme")

while True:
    # Take input from the user
    choice = input("Seçim yapınız(1/2/3/4): ")

    # Check if choice is one of the four options
    if choice in ('1', '2', '3', '4'):
        num1 = float(input("İlk numarayı giriniz: "))
        num2 = float(input("İkinci numarayı giriniz: "))

        if choice == '1':
            print(num1, "+", num2, "=", add(num1, num2))

        elif choice == '2':
            print(num1, "-", num2, "=", subtract(num1, num2))

        elif choice == '3':
            print(num1, "*", num2, "=", multiply(num1, num2))

        elif choice == '4':
            print(num1, "/", num2, "=", divide(num1, num2))

    else:
        print("Hatalı Giriş")
如果有人帮助我,我将非常感激。多谢各位


这是我的学校作业,如果你能解释更多关于代码的内容,我将不胜感激。我尽可能地提高自己,不能做这么简单的事情让我很难过,但我需要你的帮助。祝我好运,进步。注意身体健康,呆在家里

您可以通过以下方式导入系统模块并退出代码:

    def run():
        while True:
            choice = get_input()
            if choice == "a":
                # do something
            elif choice == "q":
                return

    if __name__ == "
__main__":
    run()

您可以通过以下方式导入sys模块并退出代码:

    def run():
        while True:
            choice = get_input()
            if choice == "a":
                # do something
            elif choice == "q":
                return

    if __name__ == "
__main__":
    run()

我以前做过一些菜单,在这种情况下,我最喜欢的选项是将菜单选项放在它自己的函数中,并让它将选择返回到主函数。像这样:

import sys

def run():
    # do stuff
    if choice == "q":
        sys.exit() # this exits the python program

我以前做过一些菜单,在这种情况下,我最喜欢的选项是将菜单选项放在它自己的函数中,并让它将选择返回到主函数。像这样:

import sys

def run():
    # do stuff
    if choice == "q":
        sys.exit() # this exits the python program

添加一个选项,在用户输入q时中断while循环

def multiply(number1, number2):
    return number1 * number2

def showmenu():
    print("1. Multiply")
    print("2. Quit")
    return input("Choice:")

def main():
    playing = True

    num1 = 5
    num2 = 3

    while playing:
        choice = showmenu()

        if choice == '1':
            print(f'{num1} * {num2} = {multiply(num1,num2)}')

        elif choice == '2':
            print("Quitting!")
            playing = False

if __name__ == "__main__":
    main()

添加一个选项,在用户输入q时中断while循环

def multiply(number1, number2):
    return number1 * number2

def showmenu():
    print("1. Multiply")
    print("2. Quit")
    return input("Choice:")

def main():
    playing = True

    num1 = 5
    num2 = 3

    while playing:
        choice = showmenu()

        if choice == '1':
            print(f'{num1} * {num2} = {multiply(num1,num2)}')

        elif choice == '2':
            print("Quitting!")
            playing = False

if __name__ == "__main__":
    main()
你为什么不加一个“q”选项?@DYZ当我在选项中加上“q”时,它轮到数学运算了。你为什么不加一个“q”选项?@DYZ当我在选项中加上“q”时,它轮到数学运算了