What's wrong with my code? I use 3.5.2 python. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What's wrong with my code? I use 3.5.2 python.

#!/usr/bin/python import random import time start_time = time.time() sentence = "badencryption" def randomletter(numberofletters):          # Takes a int and will returns a list     letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',                'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',                'w', 'x', 'y', 'z' ' ']     listofletters = []     index = numberofletters     while index != 0:         listofletters.append(letters[random.randint(0, 26)])         index = index - 1     return listofletters def compare(alist, astr):          # Takes a list and compares it to a str and returns a percantage     listtostr = "".join(alist)     matches = 0     index = 0     for ch in listtostr:         if ch == astr[index]:             matches = matches + 1         index = index + 1     letters = float(len(astr))     percentage = matches / letters * 100     return (percentage, listtostr) percentage = 0 bestpercentage = 0 trys = 0 while percentage < 100:     percentage, string = compare(randomletter(len(sentence)), sentence)     trys = trys + 1     if percentage > bestpercentage:         bestpercentage = percentage         print(bestpercentage)         print(string)         print(trys)          print((time.time() - start_time) / 60, "minutes")

4th Aug 2016, 3:59 PM
Pawel Dzikowicz
Pawel Dzikowicz - avatar
2 Answers
+ 1
After fixing the code, I'm still baffled about its purpose xd. It takes a while for it to complete aswell. Even as I write this, it is still ongoing. If I knew what you were trying to do, I could give some insight on how to make it more efficient. Anyway, to the what's wrong. Hard to guess when you didn't specify an error. Nonetheless I put it into my intepreter and it told me the errors in your stead. In your letters list, after z, there is ' '. It was not separated by a comma. I don't know if you meant to include this but if you did, all you need to add is the comma. PS: I actually removed it at first since I assumed you only meant for letters to be in there and then I changed the randint arguments accordingly.
4th Aug 2016, 8:56 PM
Gershon Fosu
Gershon Fosu - avatar
0
Use something like PyCharm for example. It shows you Bugs/wrong lines and can help you maybe
5th Aug 2016, 4:12 PM
Andreas Kilian
Andreas Kilian - avatar