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
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 10 26,596 May-26-2025, 01:55 PM
Last Post: deanhystad
  code not running even without errors Azdaghost 2 1,014 Apr-25-2025, 07:35 PM
Last Post: Azdaghost
  python code not running Azdaghost 1 804 Apr-22-2025, 08:44 PM
Last Post: deanhystad
  writing and running code in vscode without saving it akbarza 5 4,675 Mar-03-2025, 08:14 PM
Last Post: Gribouillis
  Python: How to import data from txt, instead of running the data from the code? Melcu54 1 1,308 Dec-13-2024, 06:50 AM
Last Post: Gribouillis
  problem in running a code akbarza 7 3,301 Feb-14-2024, 02:57 PM
Last Post: snippsat
  the order of running code in a decorator function akbarza 2 1,914 Nov-10-2023, 08:09 AM
Last Post: akbarza
  Syntax error while executing the Python code in Linux DivAsh 8 8,462 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  How to do "fixed size" (wrapping) math in Python? AlexanderWulf 13 8,309 Jul-19-2023, 04:13 PM
Last Post: deanhystad
  Code is returning the incorrect values. syntax error 007sonic 6 4,146 Jun-19-2023, 03:35 AM
Last Post: 007sonic

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.