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()
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!
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.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.