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()
Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

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

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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.