Week 3
Lists, Dictionaries, Error Handling and List Slicing
Last updated
Was this helpful?
Lists, Dictionaries, Error Handling and List Slicing
Last updated
Was this helpful?
Now that we're armed with the ability to mathematically manipulate user input, and store data in elegant structures, let's revisit and improve the login system from last time.
Let's have a go at utilizing typecasting, by asking the user to verify a simple Captcha before allowing them to try to login. Something like: "What is 4 - 2?" will suffice for now.
Store three username-password, key-value pairs in a dictionary, and use this dictionary to directly simplify the logic of username and password checking (i.e. don't store username-password information for each user in distinct variables, even for if-statement purposes).
Use a list to store some of what you'd consider "bad passwords" and then check that the usernames password doesn't match any of these - make sure this check isn't case sensitive)
Use another dictionary to store different welcome messages, corresponding to different users - print these out to users once they've successfully logged in.
Use error handling to ensure that your login system berates the user if they don't enter an actual number when attempting the Captcha (rather than just crashing and throwing a ValueError).
Using the join and split function, we're going to find a hidden message in the following poem:
hidden from all to see, trapped in an eternal night, those shadows plain surrounding me, no sunrise in sight
To do this, carry out the following:
Split the poem into a list of words
Use list slicing to create a new list containing items 0, 6, 12 ,18 from the list you created (i.e. step-size 6)
Use join() to print this list out - hopefully revealing a secret message
Take an input word from the user, and "palindromify" it - Utilize the fact that list slicing can also be similarly applied to strings
For example: "frog" -> "frogorf"
random()
ASCII character - typecast integer to character with (chr) 65-125