Question

My python code isn't working as expected

Posted January 11, 2020 332 views
Python

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()
edited by MattIPv4
1 comment

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.

×
1 answer

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.

Submit an Answer