Python Forum
Syntax error in python code sample
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Syntax error in python code sample
#1
I need to know what is the syntax error in this python code. I am using python 3.5.

# example program to demonstrate PuDB debugger; finds insertion point in
# a sorted list

def findinspt(x,xnew):  # returns insertion point of xnew in x
   n = len(x)
   lo = 0
   hi = n-1
   while True:
      mid = (lo + hi) / 2
      if xnew > x[mid]: lo = mid + 1
      else: hi = mid
      if xnew == x[mid]: return mid

y = [5,12,13]
print findinspt(y,3)
print findinspt(y,8)
print findinspt(y,12)
print findinspt(y,30)
The error is:

Error:
File "<ipython-input-1-46fa0bca0f9d>", line 15 print findinspt(y,3) ^ SyntaxError: invalid syntax
It came from this link

http://heather.cs.ucdavis.edu/~matloff/pudb.html

They seem to have no problem with the python interpreter.

Any help appreciated. Thanks in advance.

Respectfully,

ErnestTBass
Reply
#2
In python3, print() is a function and you must use parenthesis around the arguments.

As an example, line 15 should be more like

print(findinspt(y,3))
Your example is older code and was written against python2, where this behavior was different. This is the most common issue you'll run into when converting from python2 to python3. (But default behavior of division and string handling are common as well).

Looks to me like lo and hi are always integers, and 2 is an integer, so line 9 will default to integer division in python2. To keep the same behavior in python3, you'd need to replace the single slash with a double one.

      mid = (lo + hi) // 2
Reply
#3
I see your point. The tutorial said nothing about the python version so I thought it had to be python 3+. I was using python 3.7.3 instead of what I said in the initial post. I am not using Python 3.5,but python 3.7.3.

Can that one simple change in the print statement allow this code to run in Python 3.7.3? I really do not want to rewrite the code completely as python 3.7.3. But I sure can make that one simple change.

Any help appreciated. Thanks in advance.

Thanks for your help.


Respectfully,


ErnestTBass
Reply
#4
The ucdavis website looks to be ancient (2009???). There is a Python 3 compliant pudb on PyPl, pudb 2019.2.

You do not want to install Python 2.*. Also, you probably don't want to ever uninstall python *.*. Just install a new version of Python in case something is depending on the old.

Why are you trying to use pudb? There are good Python IDE's that have really good debugging tools. Any of these will be easier to use than pudb.
Reply
#5
I started using pudb. It was slow and jerky. I am not sure if I can use it. Name another good python full screen debugger. You said there are other ones available.

Respectfully,

ErnestTBass
Reply
#6
I am enjoying using Visual Studio Code. It seems simpler to use than PyCharm and much simpler than Visual Studio. All have powerful debuggers that are easy to use.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,007 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 333 Jan-19-2024, 01:20 PM
Last Post: rob101
  Code error from Fundamentals of Python Programming van Richard L. Halterman Heidi 12 1,601 Jul-25-2023, 10:32 PM
Last Post: Skaperen
  Syntax error while executing the Python code in Linux DivAsh 8 1,450 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,135 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,468 Mar-27-2023, 07:38 AM
Last Post: buran
  syntax error question - string mgallotti 5 1,248 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,194 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 847 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,777 Sep-17-2022, 04:09 AM
Last Post: jttolleson

Forum Jump:

User Panel Messages

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