如何在python中打破while循环?

问题描述 投票:0回答:4

我在这里制作一个小型计算器。接受两个数字和一个运算符这将是很容易,而我正在使用函数,但在这我使用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.')

任何人都可以帮我解决这个问题。提前致谢。

python
4个回答
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()

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

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

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.')
© www.soinside.com 2019 - 2024. All rights reserved.