Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
P2 -> P3 : Errors
#1
I have some code which I believe to be from Python 2 and need to move it to Python 3. I do not code in Python, or I stumble through very simple routines with some luck.

The code below gives me several errors in Python 3. Any suggestions or things I need to address? Any help would be greatly appreciated.


p, q = 1, 3
a, b, a1, b1 = 4, 1, 12, 4
for i in range(10000):
    p, q = p+q, q+2        # next convergent
    a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
    while 1:
        d = a//b
        r = a1 - d*b1      # break if a//b != a1//b1
        if r < 0 or r >= b1: break
        print d,           # common digits
        a, a1 = 10*(a-d*b), 10*r
Reply
#2
Python3 is usually distributed with a converter 2to3 that will attempt to find python2 code that will not run properly under python3, and may be able to fix many of them. You should try running it on the code.

For your code above it appears only to flag the print statement that becomes a function and needs a little bit of conversion.
Reply


Forum Jump:

User Panel Messages

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