Creating the Main Loop
Here’s where the magic happens. Thus far, we’ve created the two main functionalities of the program, registering and logging a user. This main loop will handle the menu system and what to show based on the user being logged in or not. Let’s go ahead and complete this program:
42| # variables for main loop
43| active = True
44| logged_in = False
46| # main loop 47| while active:
48| if logged_in:
49| print("1. Logout\n2. Quit") 50| else:
51| print("1. Login\n2. Register\n3. Quit")
53| choice = input("What would you like to do? ").lower( )
55| clear_output( ) 57| if choice == "register" and logged_in == False:
58| registerUser( ) 59| elif choice == "login" and logged_in == False:
60| logged_in = loginUser( ) 61| elif choice == "quit":
62| active = False
63| print("Thanks for using our software!") 64| elif choice == "logout" and logged_in == True:
65| logged_in = False
66| print("You are now logged out.") 67| else:
68| print("Sorry, please try again!")
Go ahead and run the cell. Before the loop starts, we define a couple variables for the program. These variables will keep track of the user being logged in and whether the program should continue to run. Then we enter the main loop and display the proper menu, depending on the user being logged in. As the user is never logged in when the program starts, the second menu will be displayed. We then ask the user what they would like to do using the input() method. The next section is where the logic of our menu system occurs. Depending on the user’s choice, we perform a specific action. We’ve made it so that the user can only log in or register if they are not already logged in. Likewise, they can only log out if they are logged in. If they choose to log in or register, we call the respective functions to perform their operations. For logging the user in, remember that the function returns True or False, which we then set the logged_in variable equal to. If the user decides to quit, we set our active variable to False and exit the program. Until then, the program will continually show the proper menu based on the user being logged in. If they choose anything but the options included, we display our error message.
Today we were able to understand the logic behind a user registration process with the use of CsV files. We’ll use similar concepts later in this book for
storing data.
Weekly Summary
Throughout this week we learned about one of the more important data collections, dictionaries. They are important when working with data as they allow us to assign key- value pairs and retrieve information at a high speed. We also covered some other data collections that serve a purpose in specific situations. After understanding collections, we were able to learn about working with files. Writing and reading from files give us the ability to add extra features to our programs, as we saw on the Friday project when we created a user registration app. We’ll be able to apply this knowledge to programs that we create later in this book.
Do'stlaringiz bilan baham: |