One, using the while loop to work with lists and dictionaries
1. Moving elements between lists
First, create a list of users to verify#and an empty list to store authenticated usersUnconfirmed_users = ['Alice','Brian','Candace']confirmed_users= []#validate each user until there are no unauthenticated users#move each validated list to the list of authenticated users whileUnconfirmed_users:current_user=Unconfirmed_users.pop ()Print("Verifying User:"+current_user.title ()) confirmed_users.append (Current_User)#Show all authenticated UsersPrint("\nthe following users have been confirmed:") forConfirmed_userinchconfirmed_users:Print(Confirmed_user.title ()) Verifying user:candaceverifying user:brianverifying user:alicethe following users have been Confirmed:candacebrianalice
Description: We first created a list of unauthenticated users (see?), which contains the user Alice, Brian, and Candace, and
An empty list was built to store the authenticated user. The while loop is running continuously until the list
The unconfirmed_users becomes empty. In this loop, the function pops () takes one at a time from the list
Unconfirmed_users end to remove unauthenticated users. Since Candace is at the end of the list unconfirmed_users,
The name will first be deleted, stored in the variable current_user, and added to the list confirmed_users (see?). Pick up
It's Brian, and then Alice. (While traversal list is unordered).
2. Delete all list elements that contain a specific value
We use the function remove () to remove a specific value from the list, which is possible because you want to delete
Value appears only once in the list. If you want to delete all the elements in the list that contain a specific value.
Pets = ['Dog','Cat','Dog','Goldfish','Cat','Rabbit','Cat']Print(Pets) while 'Cat' inchPets:pets.remove ('Cat') Print(pets) ['Dog','Cat','Dog','Goldfish','Cat','Rabbit','Cat']['Dog','Dog','Goldfish','Rabbit']
3\ using user input to populate dictionaries
responses = {}#set a flag indicating whether the investigation continuesPolling_active =True whilepolling_active:#prompt to enter the name and answer of the person being investigatedName = input ("\nwhat is your name?") Response= Input ("which mountain would you like to climb someday?")#Store the answer sheet in a dictionaryResponses[name] =Response#See if anyone else is going to take part in the investigationrepeat = input ("would another person respond? (yes/no)") ifrepeat = ='No': Polling_active=False#end of investigation, show results Print("\ n---Poll Results---") forName, responseinchResponses.items ():Print(Name +"would to climb"+ Response +".")
What is your name? Eric
Which mountain would you like to climb someday? Denali
Would another person respond? (yes/no) Yes
What is your name? Lynn
Which mountain would you like to climb someday? Devil ' s Thumb
Would another person respond? (yes/no) No
---Poll Results---
Lynn would like to climb Devil ' s Thumb.
Eric would like to climb Denali.
This program first defines an empty dictionary (responses) and sets a flag (polling_active) for
Indicate whether the investigation continues. Run the code in the while loop as long as the polling_active is True,python.
In this loop, the user is prompted to enter their user name and which mountain they like to climb (see?). Store this information in a dictionary
Responses (see?), then ask the user if the survey continues (see?). If the user enters Yes, the program will again enter
While loop, if the user enters no, the flag polling_active is set to false and the while loop ends.
The last block of code (see?) Displays the survey results.
Python Learning 7