Javascript
Posts: 12
Joined: Thu Sep 21, 2017 3:48 am

Can anyone change my Python 2.7 code to Python 3?

Thu Sep 21, 2017 3:55 am

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!'

gordon77
Posts: 4299
Joined: Sun Aug 05, 2012 3:12 pm

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

Thu Sep 21, 2017 7:37 am

Please use the code option when uploading your script, it will then keep the formatting.

What errors do you get with python 3?

jahboater
Posts: 4840
Joined: Wed Feb 04, 2015 6:38 pm

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

Thu Sep 21, 2017 7:51 am

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.

RDS
Posts: 736
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

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

Sat Sep 23, 2017 10:21 pm

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.

Martin Frezman
Posts: 1020
Joined: Mon Oct 31, 2016 10:05 am

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

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...
If this post appears in the wrong forums category, my apologies.


asandford
Posts: 1997
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

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

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

User avatar
DougieLawson
Posts: 36570
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

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

Sun Sep 24, 2017 9:46 pm

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
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Javascript
Posts: 12
Joined: Thu Sep 21, 2017 3:48 am

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

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.

asandford
Posts: 1997
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

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

Thu Sep 28, 2017 2:20 am

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.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

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

Thu Sep 28, 2017 9:40 am

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.
Last edited by elParaguayo on Thu Sep 28, 2017 11:17 am, edited 1 time in total.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

jahboater
Posts: 4840
Joined: Wed Feb 04, 2015 6:38 pm

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

Thu Sep 28, 2017 10:02 am

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.

Martin Frezman
Posts: 1020
Joined: Mon Oct 31, 2016 10:05 am

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

Thu Sep 28, 2017 10:57 am

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".
If this post appears in the wrong forums category, my apologies.

Return to “Python”