The login check from the previous exercise works, but it is not much use to whoever is typing. A wrong password only shows the question again, with no error message, so the user cannot tell the difference between a rejected attempt and a program that has frozen, and nobody ever finds out how many goes it took.
This exercise adds all three of the missing pieces: an error message, the username in the acceptance message, and a count of the attempts.
Start again from an empty editor, or from your solution to the previous exercise.
abc123 in a variable with a sensible name.1.while loop that keeps going for as long as the password entered is not
abc123. Inside the loop:
Ask each question with its own print, and read the answer with a bare
input() on the next line. Ask them with exactly this wording:
Enter your username:
Enter your password:
Both questions are printed exactly as written above, and the password question is printed again for every wrong attempt. Every wrong attempt also prints this line, so three wrong attempts print it three times:
Error, incorrect password
The last line of the output is the acceptance message. NAME stands for
whatever the user typed as their username, and NUMBER for the number of
attempts it took:
Login accepted, NAME. Number of attempts: NUMBER
Count the successful attempt as well, so a user who gets the password right straight away took 1 attempt, and a user who got it wrong three times before getting it right took 4.
Watch the punctuation in that line: a comma and a space after accepted, then a
full stop and a space after the username, then a colon and a space before the
number.
Input:
Ben
abc123
Output:
Enter your username:
Enter your password:
Login accepted, Ben. Number of attempts: 1
This user gets it wrong three times first:
Input:
Dev
123abc
ABC123
abcd123
abc123
Output:
Enter your username:
Enter your password:
Error, incorrect password
Enter your password:
Error, incorrect password
Enter your password:
Error, incorrect password
Enter your password:
Login accepted, Dev. Number of attempts: 4
An empty password counts as a wrong attempt like any other:
Input:
Kim
abc123
Output:
Enter your username:
Enter your password:
Error, incorrect password
Enter your password:
Login accepted, Kim. Number of attempts: 2
The two questions are part of the output that is checked here, so their
spelling, capital letters and punctuation count. Ask each one with its own
print, exactly Enter your username: and Enter your password:, and read
the answer with a bare input() on the next line, rather than passing the
question to input(...).
Make sure that you check for the following things:
while.while line has a condition.while line has a colon (:) at the end.while, and all of
it by the same amount. Never mix tabs and spaces in one file.input line inside the loop as well
as before it. Without them the user never gets another chance to type, so the
condition can never change and the loop never ends.str() before it is joined onto text with a +.