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
  problem in running a code akbarza 7 547 Feb-14-2024, 02:57 PM
Last Post: snippsat
  writing and running code in vscode without saving it akbarza 1 346 Jan-11-2024, 02:59 PM
Last Post: deanhystad
  the order of running code in a decorator function akbarza 2 480 Nov-10-2023, 08:09 AM
Last Post: akbarza
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 7 6,169 Aug-03-2023, 06:00 PM
Last Post: Gribouillis
  Syntax error while executing the Python code in Linux DivAsh 8 1,454 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  How to do "fixed size" (wrapping) math in Python? AlexanderWulf 13 1,734 Jul-19-2023, 04:13 PM
Last Post: deanhystad
  Code is returning the incorrect values. syntax error 007sonic 6 1,137 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  print(data) is suddenly invalid syntax db042190 6 1,122 Jun-14-2023, 02:55 PM
Last Post: deanhystad
  Math python question Deonvek 6 1,089 Apr-05-2023, 09:27 PM
Last Post: deanhystad
  Code running many times nad not just one? korenron 4 1,326 Jul-24-2022, 08:12 AM
Last Post: korenron

Forum Jump:

User Panel Messages

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