Here we finally finish the login system by making use of while loops to let the user have multiple attempts at entering the password correctly.
week3_solutions_ex1.py
# Store the username and password as key-value pairs 'username : password'users ={"alex":"1234","joe":"5678","luke":"0000"}# store the bad passwords in a listbad_passwords = ["password","pass","word","1234"]# store custom welcome messages for each user in userswelcome_msgs ={"alex":"Greetings Alex","joe":"Howdy Joe","luke":"Good day Luke"}# Add a captcha by getting the user to answer a questionanswer =input("What is 4 + 21? ")# always reads a stringanswer =int(answer)# convert string input to integer# if captcha answer was wrong, print error message and quit programif answer !=25:print("You are not human!")exit()# stops the script from being run# Now get user to enter their nameinput_name =input("Enter your username: ")input_name = input_name.lower()# Convert to lowercase# check if the username exists in our users-password dictionaryif input_name in users.keys():# input_name exists so check password matches input_password =input("Enter your password:") remaining_attempts =3# if the password is wrong and we have attempts left, ask againwhile (input_password != users[input_name]) and (remaining_attempts >0):# re-enter the captcha captcha_answer =input("Wrong password! What is 7 + 36? ") captcha_answer =int(captcha_answer)# convert to integer# captcha was wrong, so stop them tryingif captcha_answer !=43:print("You are not human!")break input_password =input("Try again:") remaining_attempts = remaining_attempts -1if input_password == users[input_name]:# password is correct, print custom welcome messageprint(welcome_msgs[input_name])# check if the password is in the bad list# use lower() to ignore case when checking for bad passwordsif input_password.lower()in bad_passwords:# password is bad so give the option to change it change_password =input("Warning: Password sucks, change?(y/n):")if change_password =="y":# user wants to change, so get a new one new_password =input("Enter new password:") users[input_name]= new_password # store for that userprint("Password changed to:"+ new_password)else:# password invalidprint("You have run out of password attempts!")else:# input_name is not in the users dictionaryprint("Unknown username")
Exercise 2.1 - Hip to be Square
Here we print a square of 'X' with a side length entered by the user
week3_solutions_ex2_1.py
# request a number, entered by the usersize =input("Enter a number: ")size =int(size)# Convert to int, assuming the input was an integer# print size amount of 'X', size times - to produce size x size squarefor number inrange(size):print("X "* size)# print hollow square# print top border - a line of X, then print middle - 'X spaces X',# then bottom borderprint("X "* size)for number inrange(size-2):print("X "+" "* (size-2) +"X ")print("X "* size)
Exercise 2.1 Extension - Hollow Square
This time we print a hollow square
week3_solutions_ext2_1.py
# request a number, entered by the usersize =input("Enter a number: ")size =int(size)# Convert to int, assuming the input was an integer# print top border - a line of X, then print middle - 'X spaces X',# then bottom border - a line of Xprint("X "* size)for number inrange(size-2)print("X "+" "* (size-2) +"X ")print("X "* size)
Exercise 2.2 - Hidden message via For Loop
Split the message into words and put into a list. Then we loop through them all and only add them to the secret message if the word begins with an uppercase character.
week3_solutions_ex2_2.py
# the secret messagemessage = "it was The best of times, it was the worst of times, it was the age of wisdom, it was the age of Foolishness, it was the epoch Of Belief, it was the epoch of incredulity, it was the season of light, it was the season of darkness, it Was The spring of Hope, it was the winter Of despair, we had everything before us, we had nothing before us, we were all Going direct To Heaven, we were all going direct the other way—in short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only."
words = message.split(" ")secret_message =""for word in words:if word[0].lower()!= word[0]: secret_message = secret_message + word +" "print(secret_message)
Exercise 2.3 - Pythagorean Triples
Here we print all the Pythagorean Triples which sum to less than 100 using for loops to calculate them. To check that c**2 = a**2 + b**2, we get the square root of a**2 + b**2 which will be a decimal value, and we check that it equals the integer version, e.g. 5.0 == 5.
week3_solutions_ex2_3.py
# the stored triplespythag_triples = []for a inrange(1, 101):for b inrange(a, 101): c = a * a + b * b# need to check that c is a square number# so we check the square root is equal to the int version of sqrtif c**0.5<100and c**0.5==int(c**0.5): pythag_triples.append([a, b, int(c**0.5)])print(pythag_triples)
Extension 1 - Goat Latin
We implement Goat Latin (adding extra "a"s to the beginning of words in repeating pattern of 1, 2, 3) by using for loops
week3_solutions_ext1.py
# Get the user input firstuser_input =input("Enter a sentence: ")result =""num_of_a =1words = user_input.split(" ")for word in words:# add num_of_a amount of a's to the beginnning of the word result = result +"a"* num_of_a + word +" "# increment number of a's, but keep value between 1 and 3 num_of_a = num_of_a +1if num_of_a >3: num_of_a =1print(result)
Extension Extra 1 - Advanced Goat Latin
This time, if a word begins with a vowel, append 'ma' to it, otherwise take the first letter, remove it from the beginning and add to the end and then append 'ma' to it. Also add an 'a' to the end of each word per index in the list, e.g. 4 a's for the fourth word.
week3_solutions_ext2.py
# the vowelsvowels = ['a','e','i','o','u']# Get the user input firstuser_input =input("Enter a sentence: ")result =""index =1words = user_input.split(" ")for word in words:# word begins with a vowelif word[0]in vowels: result = result + wordelse: result = result + word[1:]+ word[0]# now add "ma" followed by index number of 'a' result = result +"ma"+"a"* index +" " index = index +1# increment counterprint(result)