Solutions to Lists, Dictionaries, Error Handling and List Slicing
Exercise 1 - Login System MKII
Here we create a new and improved login system, by making use of the different data structures we have learnt about, dictionaries and lists.
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:")if 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 entered an incorrect password")else:# input_name is not in the users dictionaryprint("Unknown username")
Extensions
The code for the extensions can be inserted into the solution above between the preceding and subsequent lines of code -- see the comments in the code for which lines these are.
Extension 1 - Incompetent User Warning
We put the integer conversion into a try except block so that we can stop the program from reporting an error when a non-integer answer is entered.
week3_solutions_ext1.py
.............answer =input("What is 4 + 21? ")# preceding line# input always returns a string, so we need to convert it to an integer# attempt to convert the inputted string to an integertry: answer =int(answer)exceptValueError:print("You are incompetent! That was not an integer!")exit()if(answer !=25):# subsequent line.............
Extension 2 - List Slicing Fun
We take the poem as a string and split it up using the split(' ') method, splitting on spaces. Then we get every sixth word in the list using list slicing, rejoin the list together and print the result.
week3_solutions_ext2.py
poem ="hidden from all to see, trapped in an eternal night, those shadows plain surrounding me, no sunrise in sight"# split the poem into a list of wordspoem_list = poem.split(' ')# split the list on every 6th wordnew_poem_list = poem_list[::6]# create a new poem from the list of wordsnew_poem =" "# space is our separatornew_poem = new_poem.join(new_poem_list)print(new_poem)
Extension 3 - Forced Palindromes
We ask the user to input a word, and print out a palindrome version of the word, using list slicing by reversing the word and not including the first reversed character
week3_solutions_ext3.py
# Get a word from the useruser_input =input("Enter a word:")# reverse the word and don't include the first reverse characternew_word = user_input + user_input[::-1][1:]print(new_word)