Login

Back

Enter the password

Q1. Enter and run this code in Python:

1    password = "tHe+tr33s"
2    
3    counter = 1
4    guess = input("Please enter password >>> ")
5    
6    while (guess!=password) and (counter<3):
7        guess = input("Please enter password >>> ")
8        counter = counter + 1
9    
10   if guess == password:

11      print("You have entered the correct password. You may continue.")
12   elif counter >= 3:
13      print("Incorrect password. Your account has been locked. Please contact the Network Manager")
14
15   input("Press <ENTER> to quit >>>>")

Q2. Describe how this program works.
Q3. Is the password in line 1 a 'good' password? Explain your answer. 
Q4. On what line does initialisation occur?
Q5. Change line 6 to this:

while (guess!=password) or (counter<3):

Explain what happens when you run the program.
Q6.
Change line 10 to this:

if guess = password:

Explain what happens when you run the program.

Q7.  Change line 1 so that the user has to set the password first, before the rest of the program runs. You can use a simple input function for this.
Q8. Change your program so that the user has to set the password first, before the rest of the program runs. They must do this by entering in the same password twice and comparing them. If they are the same, then the new password is accepted. If they are different, then the user should be asked to re-enter the two passwords again, until they are the same.
Q9. How can this program be improved?
Q10. Why might it be a good idea to write this program as a function?

Back