Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with my code?
#1
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
Reply
#2
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.)
I am trying to help you, really, even if it doesn't always seem that way
Reply
#3
Thanks. Just to be clear how do I fix this and also whats the problem with recursion?
Reply
#4
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.
I am trying to help you, really, even if it doesn't always seem that way
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020