Problem with my code? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Problem with my code? (/thread-13136.html) |
Problem with my code? - Darbandiman123 - Sep-29-2018 Hey guys so I am writing a basic code which asks you for a password to enter. If you get the password wrong 3 times in a row then you get a time penalty. Everything works except if you get the password wrong I can’t get it to count how many times it has been wrong password = ('password') count = 0 def guessing(): guess = raw_input('Please enter the password or type cancel to go exit: ') if guess == 'cancel': print ('Cancelling..') else: password_guess(guess, password, count) def password_guess(guess, password, count): if guess == password: print ('Password correct!') print('Now opening program') else: print ('Password incorrect!') check_Count(count) def check_Count(count): count = count + 1 print (count) if count == 3: print ('You have entered the password wrong 3 times. please wait for 10 seconds') for i in range (count): print (count - i) else: guessing() guessing()Thanks in advance RE: Problem with my code? - gruntfutuk - Sep-29-2018 When you reassign a value to count in your check_Count function, you are doing so to a local variable. Check the id of count, print('initial', id(count)) , after first initialising, and also before and after the increment in the function.I also note you are using recursion (calling a function before returning from it). (As you are learning Python, I strongly recommend you use Python 3 rather than legacy Python. (Support for Python 2 ends on 1st January 2020.) Generally, I'd suggest that only experienced programmers responsible for maintaining old code, or those with a absolute dependency on a library that has not yet been updated and for which there are no Python 3 compatible alternatives should be using Python 2. Python 3 has many advances, fixed a lot of issues, and is more performant.) RE: Problem with my code? - Darbandiman123 - Sep-29-2018 Thanks. Just to be clear how do I fix this and also whats the problem with recursion? RE: Problem with my code? - gruntfutuk - Sep-29-2018 Incidentally, if you make count a list and reference [0], it will work. You might like to consider using sleep for your, er, sleep: import time time.sleep(5) There's no problem with recursion, it is just not especially useful in this case, nor entirely obvious. Why not just use a loop for the inputs and break out on a pass or exceeded count. Revised version of your code to address the local count issue, and introduce sleep. from time import sleep password = ('password') def guessing(): count = 0 while 0 <= count < 4: guess = input('Please enter the password or type cancel to go exit: ') if guess == 'cancel': print ('Cancelling..') break else: count = password_guess(guess, password, count) def password_guess(guess, password, count): if guess == password: print ('Password correct!') print('Now opening program') return -1 else: print ('Password incorrect!') count = check_Count(count) return count def check_Count(count): count += 1 if count == 3: print ('You have entered the password wrong 3 times. please wait for 10 seconds') sleep(10) return count guessing()Out of interest, what is supposed to happen after you've waited 10 seconds. The count is 3 at that point. |