Python Forum
"SyntaxError: invalid syntax" running code in Doing Math With Python b
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"SyntaxError: invalid syntax" running code in Doing Math With Python b
#1
Hello, I'm scratching my head trying to get the code in the book Doing Math With Python by Amit Saha on page 9 to work.

Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license()" for more information.
>>> from fractions import Fraction
>>> try:
    a = float(input('Enter a number: '))
    #keyword except doesn't un-indent, I have to force it with a backspace
except ValueError:
    print('You entered an invalid number')
    Enter a number 3/4
    
SyntaxError: invalid syntax
>>>
What am I doing wrong? Wrong python version?

Regards,

David
Reply
#2
Hint: Use https://pypi.org/project/ipython/

You have to indent explicit by 4 spaces after the try and the except:
>>> try:
...     a = float(input('Enter a number: '))
... except ValueError:
...     print('You entered an invalid number')
...
Enter a number: 3/4
You entered an invalid number
>>>
Same with IPython repl:
In [1]: try:
   ...:     a = float(input('Enter a number: '))
   ...: except ValueError:
   ...:     print('You entered an invalid number')
   ...:
Enter a number: 3/4
You entered an invalid number

In [2]:
The benefit here is, that the indentation for multiline is made automatically.
You don't have to hit 4 times the space, if you use IPython. Also Tabulator is converted into 4 spaces :-)
saucerdesigner likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
(Nov-03-2020, 03:56 PM)DeaD_EyE Wrote: Hint: Use https://pypi.org/project/ipython/

You have to indent explicit by 4 spaces after the try and the except:
>>> try:
...     a = float(input('Enter a number: '))
... except ValueError:
...     print('You entered an invalid number')
...
Enter a number: 3/4
You entered an invalid number
>>>
Same with IPython repl:
In [1]: try:
   ...:     a = float(input('Enter a number: '))
   ...: except ValueError:
   ...:     print('You entered an invalid number')
   ...:
Enter a number: 3/4
You entered an invalid number

In [2]:
The benefit here is, that the indentation for multiline is made automatically.
You don't have to hit 4 times the space, if you use IPython. Also Tabulator is converted into 4 spaces :-)

Thank you! Evidently I don't yet know how to run IDLE, so I tried PyCharm Edu and it ran ok. Thanks again1
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  SyntaxError: invalid syntax ?? korenron 8 91 3 hours ago
Last Post: snippsat
  My python code is running very slow on millions of records shantanu97 8 334 Jan-20-2022, 02:09 PM
Last Post: Hnry_Boyd
  Python keeps running the old version of the code quest 2 90 Jan-20-2022, 07:34 AM
Last Post: ThiefOfTime
  Invalid syntax with an f-string Mark17 7 259 Jan-14-2022, 04:44 PM
Last Post: Mark17
  I am getting ValueError from running my code PythonMonkey 0 149 Dec-26-2021, 06:14 AM
Last Post: PythonMonkey
  invalid syntax in my class CompleteNewb 2 331 Dec-13-2021, 09:39 AM
Last Post: Larz60+
  Math Package in python Uma 1 235 Dec-12-2021, 02:01 PM
Last Post: jefsummers
Exclamation Invalid syntax error(Predict Ethereum Price) lulu43366 2 668 Sep-24-2021, 01:24 PM
Last Post: lulu43366
  Unexplained Invalid syntax Error cybertooth 5 886 Aug-02-2021, 10:05 AM
Last Post: cybertooth
Sad SyntaxError: from simple python example file from mind-monitor code (muse 2) warmcupoftea 4 814 Jul-16-2021, 02:51 PM
Last Post: warmcupoftea

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020