如何在python中打破while循环?

时间:2018-09-01 16:52:42

标签: python

在这里,我正在做一个小型计算器。接受两个数字和一个运算符,这在我使用函数时会很容易,但是在这种情况下,我使用while条件语句,但是有一个错误,它不会中断,而每次操作都会询问用户是否需要再次在“ Y”中表示是,“ N”表示否,但有一个错误,它不会更改n的值。下面是我的程序:

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

有人可以帮我解决这个问题吗?预先感谢。

4 个答案:

答案 0 :(得分:1)

您在number中使用了局部变量again,但在外部使用了n。您必须将again的返回值分配给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()

答案 1 :(得分:0)

如果要中断循环,只需在要停止循环的位置使用break

编辑:
您的循环可以像:

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

答案 2 :(得分:0)

无需存储值n

  1. while循环更改为while True:

  2. 更改again函数以返回布尔值。

  3. 在调用again函数时使用以下语法。

    if not again():
           break
    

无需存储值n

  1. while循环更改为while True:

  2. 更改again函数以返回布尔值。

  3. 在调用again函数时使用以下语法。

    if not again():
           break
    

最终的代码将是这样。

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

答案 3 :(得分:0)

您可以按如下方式简化代码,并避免代码中不必要的递归:

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