Description
Material covered
- Conditional statements
while
loops
Exercises
Question 1: Password checking
You are presented with a file passwords.txt
where each line contains an email address followed by a password. The email address is separated from the password by a comma.
Your job is to write a Python script (starting with the template lab6q1.py
) to flag all passwords that are not secure (or insecure).
A password is considered secure if it satisfies the following conditions:
- contains at least 8 characters,
- contains at least one numeric digit, and
- contains at least alphabet character.
Otherwise, a password is not secure.
Your Python script that will read in the file passwords.txt
and for each insecure password, print the following information: * the line number in the file * the email address * the password * condition(s) password failed to be secure
At the end, you should also print the number of email addresses with insecure passwords.
For example, suppose the file passwords.txt
contain the following lines:
Ben.Li@umanitoba.ca,abc123
Joe.Blow@yahoo.com,ay799dkZ!
John.Wong@gmail.com,mrcool
Row.Fernando@hotmail.com,12345789
Sheikh.Jubair@umanitoba.ca,ntyd888896
Olivia.Li@gmail.com,8739!
Then the output should look like:
Invalid Passwords Detail:
1) Ben.Li@umanitoba.ca,abc123:less than 8 characters
3) John.Wong@gmail.com,mrcool:less than 8 characters, no digit
4) Row.Fernando@hotmail.com,12345789:no alphabet character
6) Olivia.Li@gmail.com,8739!:less than 8 characters, no alphabet character
There were 4 emails with insecure passwords.
The file lab5q1.py
contains two functions hasNumbers(s)
and hasAlpha(s)
which returns True
if and only if s
has a digit and alphabet character, respectively.
Question 2: Deduction Game
Write a game where a user has to guess a number from 1 – 99.
Your program will generate a random number once (pick a number), and will then prompts the user to repeatedly guess the number until they get it correct.
During the game, your program will test the number that the user entered compared to the number it picked. If the number that the user guessed is greater than the random number, tell the user “your guess is too low”. If the number that the user guessed is higher than the random number, tell the user, “your guess is too high”. Let the user continue guessing the number generated until they guess it right.
Once the user has successfully guessed the number, tell the user they win, and tell them how many guesses it took them to guess it right.
Use a loop to check your user input. Use string’s .isnumeric() to check to see if the user has provided you with a valid number before you use a cast to an integer. Your program should not crash if the user gives you bad input.
To create a random number use:
import random
myRandomNumber = random.randint(1, 99)
Sample game:
I've picked a number between 1 and 99, can you guess it? 50 Nope, it's not 50 Your guess is too low I've picked a number between 1 and 99, can you guess it? 99 Nope, it's not 99 Your guess is too high I've picked a number between 1 and 99, can you guess it? 42 Nope, it's not 42 Your guess is too low I've picked a number between 1 and 99, can you guess it? 93 Nope, it's not 93 Your guess is too high I've picked a number between 1 and 99, can you guess it? 83 You got it! It took you 5 guesses.
Optional: New and Improved Deduction Game
Once you’ve completed the guessing game, add some usability improvements. In the line that prompts the user for input, print the known range the random number could be in.
Do not increase the range if the user guesses outside your range. Example, if the current range is 40 to 60, do not change them if the user guesses 100.
Example:
Guess the number. 0 < _ < 100: 50 Guess the number. 0 < _ < 50: 25 Guess the number. 0 < _ < 25: 12 Guess the number. 12 < _ < 25: 18 Guess the number. 12 < _ < 18: 14 Guess the number. 14 < _ < 18: 16 Guess the number. 16 < _ < 18: 17 Congrats you guessed it in 7 tries
Challenge: What’s the fewest number of guesses you have to make for any range of numbers? Hint: This is a search algorithm called a binary search.