~ Solve Problems and Learn how to code in Python


8 - Introducing the use of Dictionaries (to solve the same registration and login problem)

Here's the ultimate introduction to Dictionaries

Go through this information (come back to it as and when you need) and then attempt the challenges

Challenge - use dictionaries to solve this problem

First, watch the demo video below. Your task is to code something like this, and use the code below to get started

You can either edit and code online in repl.it or you can simply cut and paste the below into your own python file

#TASK: Understand how a dictionary is sometimes an amazing solution (works better than two lists)
#****1. SELECT REGISTRATION AND ADD MORE USERS
#*** 2. GO THROUGH THE CODE AND NOTE HOW THE DICTIONARY HAS BEEN CREATED with key value pairs
#****3. Note the code to search a dictionary in the login function, to add to the dictionary in the reg function and how to print out a dictionary and its items in the registration_details function
usernames_passwords={"user1":"pass1","user2":"pass2","user3":"pass3"}

def main():
   mainmenu()


def mainmenu():
   print("****MAIN MENU****")
   print("=======Press L to login :")
   print("=======Press R to register :")
   choice1=input()
   if choice1=="L" or choice1=="l":
      login()
   elif choice1=="R" or choice1=="r":
      register()
   else:
      print("please make a valid selection")

def login():
   print("*****LOGIN SCREEN******")
   username=input("Username: ")
   password=input("Password: ")
   if username not in usernames_passwords:
      print("The user does not exist")
   elif usernames_passwords[username]==password:
      print("That's fine - access granted!")
   elif usernames_passwords[username] !=password:
      print("Sorry - wrong combination")
   

def register():
   print("*****REGISTRATION****")
   username=input("Enter a username:")
   password=input("Enter a password:")
   usernames_passwords[username]=password #this adds a username and password key value pair to the existing dictionary
   answer=input("Do you want to make another registration?")
   if answer=="y":
      register()
   else:
      registration_details()

def registration_details():
   print(usernames_passwords) #note that a dictionary does not store the items in any particular sorted order
   #****CAN ALSO BE PRINTED OUT USING A FOR LOOP BELOW
   #for k, v in usernames_passwords.items():
      #print(k,v)
main()

Code your solution here

Systems Life Cycle (in a nutshell): Analyse - Design - Create - Test - Evaluate. Designing something or writing out some pseudocode before you actually write code is always a good idea! Get in to the habit of doing so! You can draw your flowchart here and screenshot it.

A sample flow chart (design) for this particular challenge could look like:

Flowchart: Python program to get the Fibonacci series between 0 to 50
Each challenge section below provides an online drawing tool where you can dynamically create flowcharts. Screenshot them into your presentation for submission.

Solutions & Answers

Answers /Solutions in the "members area" drive under: "Solve and Learn >>SOLUTIONS"

Testing Table

You may want to read a little about Testing first. A teacher may go through some examples with you. Feel free to fill in the test table here, and screenshot it in to your powerpoint. Testing is absolutely essential once you have created a program!
Test No. Description Test Data(input) Expected Outcome Actual Outcome Further Action?
1
2
3
4
5
Coming soon!