Не могу выйти из программы - PullRequest
1 голос
/ 17 февраля 2020

Попытка выхода из программы путем импорта sys.exit (), break и ask == False, но ничего не работает. Полный код здесь

#import sys


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 division_option(*args):

    return first_number / second_number


begin = " " 

while begin:

    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))


ask = " "

while ask:

    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

1 Ответ

0 голосов
/ 17 февраля 2020

Вы просто хотите заменить 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!"

Остальное можно поместить в один l oop вместо двух, например:

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.\n
                    Choose 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'.\n
                              Choice: ").lower()

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

    elif exit_or_continue == "n":
       break
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...