无法退出程序

问题描述 投票:1回答:1

尝试通过导入sys.exit()退出程序,中断并询问== False,但没有任何效果。完整代码here

导入系统

def body_cycle(* args):

if option == "/":
    error_func_for_zero(first_number, second_number, option)
    print(division_option(first_number, second_number))
    print()
    print(begin)

def error_func_for_zero(* args):

try:
   first_number / 0 or second_number / 0

except ZeroDivisionError:
   print("YOU CANNOT DIVIDE BY ZERO!")
   print(begin)

def除法选项(* args):

return first_number / second_number

开始=“”

开始时:

print("Hello, I am calculator. ")  
print("Please, enter your numbers (just integers) ! ")

print()

first_number = int(input("First number: "))

print()

second_number = int(input("Second number: "))

print()

option = input("Remember: you can't divide by zero.\nChoose your option (+, -, *, /): ")

print(body_cycle(first_number, second_number, option))

问=“”

同时询问:

exit_or_continue = input("If you want continue press 'Y', 'y'. For break press 'N' or 'n'? \nChoice: ")

if exit_or_continue == "Y" or "y":
   print("OK")

elif exit_or_continue == "N" or "n":  
   #break
   ask == False

else:
   print("Break program. ")
   break
python-3.x exit continue
1个回答
0
投票

您只想将ask == False替换为ask = False

此外,您实际上可以使用更简单的代码结构。 begin之前的整个内容可以压缩为:

def operation(a, b, option):

    if option == "+":
        return a + b

    elif option == "-":
        return a - b

    elif option == "*":
        return a * b

    elif option == "/":
        try:
            return a / b
        except ZeroDivsionError
            return "YOU CANNOT DIVIDE BY ZERO!"

其余的可以放在一个循环中,而不是两个,就像这样:

print("Hello, I am calculator. ")  

while True:
    print("Please, enter your numbers (just integers) ! ")
    print()

    first_number = int(input("First number: "))
    print()

    second_number = int(input("Second number: "))
    print()

    option = input("Remember: you can't divide by zero.\nChoose your option (+, -, *, /): ")

    # Result.
    print(operation(first_number, second_number, option))

    exit_or_continue = input("If you want continue press 'Y', 'y'. For break press 'N' or 'n'? \nChoice: ").lower()

    if exit_or_continue == "y":
       print("OK")

    elif exit_or_continue == "n":
       break
© www.soinside.com 2019 - 2024. All rights reserved.