Tutorial for first year computer science students

Back to Tutorials

User Login - Dictionaries and Tuples

Learning Objectives

We are going to learn:

  1. How to use dictionaries in Python.
  2. How to use tuples in Python.

Exercises

Download the Medialib ex9 code files and 5 image files (as shown below) from the gallery and complete the program in ex9.py.

MediaLib Dice Exercise

With a dictionary that have usernames as keys and passwords as values, which is like login_dict={“taro”: “1234”, “hanako”: ”asdf”, ...}, create a user login program that loops the following processes until the user successfully logged in.

  1. On lines 18-19, display “login.png”, and use input() function with a prompt “Username: ” to get the username.
  2. If the username exists in the dictionary, from line 20 display “password.png” and use input() function with a prompt “Password: ” to get the password. If the password is correct, print ”Login succeeded!", and display “welcome.png”, then finish the program. Otherwise, print “Password is incorrect!, display “wrong_password.png” and wait for a mouse left click to go to 1. again.
  3. If the username does not exist, then print “No such user!”, and display “wrong_user.png”, then wait for a mouse left click to go to 1. again
MediaLib Exercise




Creative Commons Licence
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.