(Jul-19-2017, 10:39 AM)Gamervote Wrote: def ispass():
if "".join(guess) == password:
return True
else:
return False
You don't need the if blocks...
def ispass(): return "".join(guess) == passwordBut that's besides the point. There's a pretty cool profiler included in the standard lib, which can help narrow down where you should focus on improvement: https://docs.python.org/3.6/library/profile.html
As a demo, I changed your program slightly so that the while loop at the end is a function, and then I call that function with the profiler.
The code:
# imports (im not insulting your inteligence, im just new. :) ) import string import time # sets printables var = 3 if var == 1: printable = list(string.printable) for i in range(5): del printable[-1] elif var == 2: printable = list(str(string.ascii_uppercase + string.ascii_lowercase)) elif var == 3: printable = list(string.ascii_lowercase) # variable assignment password = input("What is the password? -> ") guess = [printable[0]] place = 0 limit = 8 correct = False # sees if the current guess is correct, returns true if so def ispass(): if "".join(guess) == password: return True else: return False # reverts all elements of guess list to first element of printables def zitup(): for i in range(len(guess)): guess[i] = printable[0] guess.append(printable[0]) # moves inncorrect guess to the next string def next(): if guess[place] == printable[-1]: guess[place] = printable[0] else: try: guess[place] = printable[printable.index(guess[place]) + 1] except ValueError: print(guess, place) # if ispass() returns false detects if all elements of guess are the last element of printable, if so exicutes zitup(), else sets the next non last element of printables to the next possible element place = 0 def find_password(): global place while len(guess) < limit: correct = ispass() if correct: break else: place = len(guess) - 1 while guess[place] == printable[-1] and place > -1: guess[place] = printable[0] place += -1 if place < 0: zitup() print("Char added") else: next() import cProfile cProfile.run("find_password()") # prints correct guess, or incorrect if len(guess) exceded limit print("The password is: " + "".join(guess))
Output:What is the password? -> spam
Char added
Char added
Char added
2068813 function calls in 0.767 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.767 0.767 <string>:1(<module>)
6 0.000 0.000 0.000 0.000 cp437.py:18(encode)
344799 0.094 0.000 0.137 0.000 eggs.py:29(ispass)
3 0.000 0.000 0.000 0.000 eggs.py:35(zitup)
344795 0.193 0.000 0.312 0.000 eggs.py:43(next)
1 0.284 0.284 0.767 0.767 eggs.py:57(find_password)
6 0.000 0.000 0.000 0.000 {built-in method _codecs.charmap_encode}
1 0.000 0.000 0.767 0.767 {built-in method builtins.exec}
689600 0.033 0.000 0.033 0.000 {built-in method builtins.len}
3 0.000 0.000 0.000 0.000 {built-in method builtins.print}
3 0.000 0.000 0.000 0.000 {method 'append' of 'list' objects}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
344795 0.119 0.000 0.119 0.000 {method 'index' of 'list' objects}
344799 0.043 0.000 0.043 0.000 {method 'join' of 'str' objects}
The password is: spam
I don't have the patience to wait for a 5 character input, but we can already see that there are a massive number of calls to ispass() and next(), which take up most of the running time. Those functions aren't terribly complicated, which means the focus of improvement shouldn't be on making those functions better, but instead should focus almost entirely on having less loops. Specifically, if you can get rid of the while loop that's within the while loop, you'd likely see much better performance.