By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 883 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

Python Calculator not accepting proper operations

P: 2
I was trying to make a advanced calculator in Python. The calculator had been working fine until I added my "operation" variable. I am unsure if this has to do with my computer running Linux Mint Mate as it's OS. I have checked through my code multiple times and couldn't find anything wrong with it "Note: I am mildly new to Python and have only been learning it for about a month" When the user tries to enter a operation, it responds with:
Expand|Select|Wrap|Line Numbers
  1. Traceback (most recent call last):
  2.   File "pythoncalculator.py", line 7, in <module>
  3.     ''')
  4.   File "<string>", line 1
  5.     +
  6.     ^
  7. SyntaxError: unexpected EOF while parsing
If the user enters a whole equation then it will give them the option to enter the numbers that are going to be used in the equation. When the user does this they get:
Expand|Select|Wrap|Line Numbers
  1. Please enter the math operation you want to do:
  2.  + Add
  3.  - Subtract
  4.  * Multiply
  5.  / Divide 
  6.  5 + 5
  7. Enter first number: 5
  8. Enter second number: 5
  9. You have failed to type in a proper operation. Please restart program.
This shows that it does recognize that the number is not a choice for the operation, but when a proper answer choice is entered it will not work "See the Syntax Error above to know what happens" I have also included a large portion of my code, if anyone can see whats wrong please respond.
Expand|Select|Wrap|Line Numbers
  1. operation = input('''
  2. Please enter the math operation you want to do:
  3.  + Add
  4.  - Subtract
  5.  * Multiply
  6.  / Divide 
  7.  ''')
  8. num1 = int(input('Enter first number: '))
  9. num2 = int(input('Enter second number: '))
  10.  
  11. if operation == '+':
  12.     print('{} + {} = '.format(num1, num2))
  13.     print(num1 + num2)
  14.  
  15. elif operation == '-':
  16.     print('{} - {} = '.format(num1, num2))
  17.     print(num1 - num2)
  18.  
  19. elif operation == '*':
  20.     print('{} * {} = '.format(num1, num2))
  21.     print(num1 * num2)
  22.  
  23. elif operation == '/':
  24.     print('{} / {} = '.format(num1, num2))
  25.     print(num1 / num2)
  26.  
May 1 '17 #1

✓ answered by dwblas

The error message there are triple quotes after the 3

Expand|Select|Wrap|Line Numbers
  1.   File "pythoncalculator.py", line 7, in <module>
  2.     ''')
  3.                          3     ''') <----- 
and then you continue with 4 on the next line, which is not part of the literal anymore since it ended on the "3 option" line above it.

Share this Question
Share on Google+
1 Reply


Expert 100+
P: 614
The error message there are triple quotes after the 3

Expand|Select|Wrap|Line Numbers
  1.   File "pythoncalculator.py", line 7, in <module>
  2.     ''')
  3.                          3     ''') <----- 
and then you continue with 4 on the next line, which is not part of the literal anymore since it ended on the "3 option" line above it.
May 1 '17 #2

Post your reply

Sign in to post your reply or Sign up for a free account.