繁体   English   中英

如何在python中打破while循环?

[英]How to break a while loop in 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.')

谁能帮我解决这个问题。 提前致谢。

again使用局部变量number ,但在外部使用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()

如果要中断循环,只需在要停止循环的位置使用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.')

无需存储值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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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