0

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.

2
  • Just as you do in C, C++ or Java, use break. Commented Sep 1, 2018 at 16:59
  • A side note: a rule of thumb for naming your functions: if you can explain in few words what it does, use those words as the function name, if you cannot, delete the whole function - it is not doing anything sensible. A function called again is always a bad idea. Commented Sep 1, 2018 at 17:01

4 Answers 4

1

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()
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

you are saying i have to use if statement?
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.