简体   繁体   中英

How to break a while loop in python?

Here I m making a small calculator. accepting two numbers and one operator this will be easy while I'm using function but in this I'm using the while condition statement but there is a error it will not breaking while every operation it will ask to user that it will want to any operation again in 'Y' for yes and 'N' for no but There is an error it will not changing the value of n. Below is my program:

n = 1
def again(number):
    print('value of n in again fucntion', n)
    calc_again = input('''
Do you want to calculate again?
Please type Y for YES or N for NO.
''')

    if calc_again.upper() == 'Y':
        number = 1
        return number
    elif calc_again.upper() == 'N':
        number = 0
        print('value of n after say no', number)
        return number
    else:
        again(n)
while n > 0:
    print('while n value', n)
    operation = input('''
Please type in the math operation you would like to complete:
+ for addition
- for subtraction
* for multiplication
/ for division
''')
    number_1 = int(input('Please enter the first number: '))
    number_2 = int(input('Please enter the second number: '))

    if operation == '+':
        print('{} + {} = '.format(number_1, number_2))
        print(number_1 + number_2)
        again(n)

    elif operation == '-':
        print('{} - {} = '.format(number_1, number_2))
        print(number_1 - number_2)
        again(n)

    elif operation == '*':
        print('{} * {} = '.format(number_1, number_2))
        print(number_1 * number_2)
        again(n)

    elif operation == '/':
        print('{} / {} = '.format(number_1, number_2))
        print(number_1 / number_2)
        again(n)

    else:
        print('You have not typed a valid operator, please run the program again.')

Can anybody please help me for solving this. Thanks in advance.

You use the local variable number in again , but use n outside. You have to assign the return value of again to n .

def again():
    while True:
        calc_again = input('''
Do you want to calculate again?
Please type Y for YES or N for NO.
''')
        if calc_again.upper() == 'Y':
            number = 1
            return number
        elif calc_again.upper() == 'N':
            number = 0
            print('value of n after say no', number)
            return number

n = 1
while n > 0:
    print('while n value', n)
    operation = input('''
Please type in the math operation you would like to complete:
+ for addition
- for subtraction
* for multiplication
/ for division
''')
    number_1 = int(input('Please enter the first number: '))
    number_2 = int(input('Please enter the second number: '))

    if operation == '+':
        print('{} + {} = '.format(number_1, number_2))
        print(number_1 + number_2)
    elif operation == '-':
        print('{} - {} = '.format(number_1, number_2))
        print(number_1 - number_2)
    elif operation == '*':
        print('{} * {} = '.format(number_1, number_2))
        print(number_1 * number_2)
    elif operation == '/':
        print('{} / {} = '.format(number_1, number_2))
        print(number_1 / number_2)
    else:
        print('You have not typed a valid operator, please run the program again.')
    n = again()

If you want to break your loop, then simply use break where you want the loop to stop.

Edit:
Your loop can be like:

while n > 0:
    print('while n value', n)
    operation = input('''
Please type in the math operation you would like to complete:
+ for addition
- for subtraction
* for multiplication
/ for division
''')
    number_1 = int(input('Please enter the first number: '))
    number_2 = int(input('Please enter the second number: '))

    if operation == '+':
        print('{} + {} = '.format(number_1, number_2))
        print(number_1 + number_2)
        if again(n) == 0:break

    elif operation == '-':
        print('{} - {} = '.format(number_1, number_2))
        print(number_1 - number_2)
        if again(n) == 0:break

    elif operation == '*':
        print('{} * {} = '.format(number_1, number_2))
        print(number_1 * number_2)
        if again(n) == 0:break

    elif operation == '/':
        print('{} / {} = '.format(number_1, number_2))
        print(number_1 / number_2)
        if again(n) == 0:break

    else:
        print('You have not typed a valid operator, please run the program again.')

There is no need to store a value n

  1. Change the while loop to while True:

  2. Change the again function to return a Boolean.

  3. use the following syntax when calling the again function.

     if not again(): break 

There is no need to store a value n

  1. Change the while loop to while True:

  2. Change the again function to return a Boolean.

  3. use the following syntax when calling the again function.

     if not again(): break 

The final code will be something like this.

def again():
    calc_again = input('''
Do you want to calculate again?
Please type Y for YES or N for NO.
''')

    if calc_again.upper() == 'Y':
        return True
    elif calc_again.upper() == 'N':
        return False
    else:
        return again()

while True:
    operation = input('''
Please type in the math operation you would like to complete:
+ for addition
- for subtraction
* for multiplication
/ for division
''')
    number_1 = int(input('Please enter the first number: '))
    number_2 = int(input('Please enter the second number: '))

    if operation == '+':
        print('{} + {} = '.format(number_1, number_2))
        print(number_1 + number_2)

    elif operation == '-':
        print('{} - {} = '.format(number_1, number_2))
        print(number_1 - number_2)

    elif operation == '*':
        print('{} * {} = '.format(number_1, number_2))
        print(number_1 * number_2)

    elif operation == '/':
        print('{} / {} = '.format(number_1, number_2))
        print(number_1 / number_2)

    else:
        print('You have not typed a valid operator, please run the program again.')
        break

    if not again():
        break

You can simplify your code as follow, and to avoid unnecessary recursion in the code:

operations = {
    "+": lambda x, y: x + y,
    "-": lambda x, y: x - y,
    "/": lambda x, y: x / y,
    "*": lambda x, y: x * y
}
continue_calculation = ""
while True:
    calc_again = input('''Do you want to calculate again?Please type Y for YES or N for NO.''')
    if calc_again == "n" or calc_again == "N":
        break
    operation = input('''Please type in the math operation you would like to complete:
                            + for addition
                            - for subtraction
                            * for multiplication
                            / for division
                          ''')
    number_1 = int(input('Please enter the first number: '))
    number_2 = int(input('Please enter the second number: '))
    try:
        print(operations[operation](number_1, number_2))
    except:
        print('You have not typed a valid operator, please run the program again.')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM