Question

My python code isn't working as expected

What did I do wrong?

def calculate():
    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('Enter your first number :' ))
number_2 = int(input('Enter your 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,')
    

    again()


calculate()


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':
        calculate()


    elif calc_again.upper() == 'N':
        print('See you later.')


    else:
        again()


calculate()
Subscribe
Share

Hi there @vahegrigoryan5y,

Would you be able to provide further explanation on what you are expecting to this code to do, as well as where it is going wrong currently?

- Matt.


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there @vahegrigoryan5y,

Without any further information about the issue you were encountering, it is hard to suggest a solution here.

However, it might be that you’ve got some parts of your code indented incorrectly, assuming the code you’ve supplied here is exactly what you’re running.

If you reindent all your code correctly, the program appears to run correctly. You can see my cleaned-up version of your code on Repl.it here: https://repl.it/@MattIPv4/calculator-do-question

def calculate():
    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('Enter your first number :'))
    number_2 = int(input('Enter your 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,')
      again()

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':
      calculate()

    elif calc_again.upper() == 'N':
      print('See you later.')

    else:
      again()

calculate()

Hope that helps! - Matt.