Page 1 of 1

Can anyone change my Python 2.7 code to Python 3?

Posted: Thu Sep 21, 2017 3:55 am
by Javascript
The title says it all. Thank you in advanced!

Code: Select all

import time
import string
 
start = time.time()
chars = list(string.printable)[:95]
base = len(chars)
n = 0
password = '123456'
solved = False
 
if password == '':
    print 'Your password is empty'
    solved = True
elif password == chars[n]:
    print 'Your password is ' + chars[n]
    solved = True
else:
    n += 1
 
 
def numberToBase(n, b):
    digits = []
    while n:
        digits.append(int(n % b))
        n /= b
    return digits[::-1]
 
 
if not solved:
    while n < 99999999:
        lst = numberToBase(n, base)
        word = ''
        for x in lst:
            word += str(chars[x])
        # print word
        if password == word:
            solved = True
            print '-Stats-'
            print 'Pass: ' + word
            print 'Attempts: ' + str(n)
            print 'time: ' + str((time.time() - start)) + ' sec'
            break
        else:
            n += 1
if not solved:
    print 'Unsolved after ' + str(n) + ' attempts!'

Re: Can anyone change my Python 2.7 code to Python 3?

Posted: Thu Sep 21, 2017 7:37 am
by gordon77
Please use the code option when uploading your script, it will then keep the formatting.

What errors do you get with python 3?

Re: Can anyone change my Python 2.7 code to Python 3?

Posted: Thu Sep 21, 2017 7:51 am
by jahboater
There is plenty of information about this, for example:

http://sebastianraschka.com/Articles/20 ... _diff.html

Start by changing

print 'sdsdds'

to

print( 'sdsdds' )

Then check for / which probably needs to be // to get integer floor division.

Re: Can anyone change my Python 2.7 code to Python 3?

Posted: Sat Sep 23, 2017 10:21 pm
by RDS
When I needed to convert a program from python 2 to Python 3, I found it was relatively easy to just run the Python 2 program, in a Python 3 editor and sort out the errors one by one. You can then always come back on here if you get a specific error that you cannot sort out.

Re: Can anyone change my Python 2.7 code to Python 3?

Posted: Sat Sep 23, 2017 10:31 pm
by Martin Frezman
Doesn't it seem like someone should write an automated converter program (if such has not already been done) ?

I wonder what googling for "convert python 2 to python 3" would reveal...

Re: Can anyone change my Python 2.7 code to Python 3?

Posted: Sat Sep 23, 2017 10:43 pm
by Douglas6

Re: Can anyone change my Python 2.7 code to Python 3?

Posted: Sun Sep 24, 2017 9:43 pm
by asandford
Surely the whole point of the RPF is to encourage research and experimentation?

Too many 'please do this (homework) for me' threads at the moment

Re: Can anyone change my Python 2.7 code to Python 3?

Posted: Sun Sep 24, 2017 9:46 pm
by DougieLawson
asandford wrote:
Sun Sep 24, 2017 9:43 pm
Surely the whole point of the RPF is to encourage research and experimentation?

Too many 'please do this (homework) for me' threads at the moment
https://en.wikipedia.org/wiki/Eternal_September

Re: Can anyone change my Python 2.7 code to Python 3?

Posted: Thu Sep 28, 2017 12:44 am
by Javascript
Martin Frezman wrote:
Sat Sep 23, 2017 10:31 pm
Doesn't it seem like someone should write an automated converter program (if such has not already been done) ?

I wonder what googling for "convert python 2 to python 3" would reveal...
I tried to use the converting program that Python had. I asked on this forum because it didn't work.
Sorry if I bothered you.

Re: Can anyone change my Python 2.7 code to Python 3?

Posted: Thu Sep 28, 2017 2:20 am
by asandford
Javascript wrote:
Thu Sep 28, 2017 12:44 am
I tried to use the converting program that Python had. I asked on this forum because it didn't work.
Sorry if I bothered you.
You should have asked on the forum of whatever tool you used.

Unless you are using something very specific to the Pi, then there are probably better python forums (fora?) that you could ask.

The vast majority of users on here are just like you, and aren't paid for responding, they try to help others (showing some effort to solve your problem goes a long way as well). You get way more help here than the support contract you signed cost (£0.00)

You program is ~40 lines, and as said before, run it with python3 and correct the errors as you go.

Re: Can anyone change my Python 2.7 code to Python 3?

Posted: Thu Sep 28, 2017 9:40 am
by elParaguayo
Javascript wrote:
Thu Sep 28, 2017 12:44 am
Martin Frezman wrote:
Sat Sep 23, 2017 10:31 pm
Doesn't it seem like someone should write an automated converter program (if such has not already been done) ?

I wonder what googling for "convert python 2 to python 3" would reveal...
I tried to use the converting program that Python had. I asked on this forum because it didn't work.
Sorry if I bothered you.
Can you explain what you mean by "it didn't work"? Other than the usual change required from python 2's print statements to python 3's print function (i.e. you need to use parentheses - see jahboater's earlier post).

Changing this causes the code to run with no errors but I'm not sure I'd call it "working" as it just runs and runs with no output. However, that's a different question and nothing to do with python 2 vs python 3!

EDIT: That last sentence is probably unfair and untrue. There could be differences that don't give rise to explicit errors but change the behaviour.

Re: Can anyone change my Python 2.7 code to Python 3?

Posted: Thu Sep 28, 2017 10:02 am
by jahboater
I suspect you will need to add a / in numberToBase()
n /= b
should be
n //= b
to retain the integer floor division that Python2 did.

Re: Can anyone change my Python 2.7 code to Python 3?

Posted: Thu Sep 28, 2017 10:57 am
by Martin Frezman
Yes, that looks like the answer (to both my own and the OP's initial queries). It seems to me that both of the following must be true:

1) That program does an almost, but not quite, complete job. It sounds like there is still room for future improvement (hence the emphasis on the ability to add more "fixers" in the future).

2) The job itself is not 100% analytic - i.e., it may not be possible to do it completely and in all cases. Hence the fact that the program is not quite "done".