5-8. Say hello to the administrator in a special way: create a list of at least 5 user names, one of which is' admin '. Imagine writing code to print a greeting message after each user logs in to the website. Traverse the list of user names and print a greeting message to each user.
·If the user name is' admin ', a special greeting message will be printed, such as "Hello admin, would you like to see a status report?".
·Otherwise, print a normal greeting message, such as "Hello. Eric, thank you. for. logging. in. again".
user_name=['xuyan','yuhangli','jiayanghou','jiachenyang','baoyusong','admin'] for user in user_name: if user=='admin': print('Hello admin,would you like to see a status report?') else: print('Hello '+user+',thank you for logging in again.')
5-9} deal with the situation of no user: in the program written to complete exercise 5-8, add an if statement to check whether the user name list is empty.
·If it is empty, the message "We need to find some users!" is printed.
·Delete all user names in the list and make sure that the correct message will be printed.
user_name=['xuyan','yuhangli','jiayanghou','jiachenyang','baoyusong','admin'] for user in user_name: if user=='admin': print('Hello admin,would you like to see a status report?') else: print('Hello '+user+',thank you for logging in again.') del user_name[:] if user_name == : print('We need to find some users!')
5-10 # check user name: write a program according to the following instructions to simulate the website and ensure that each user's user name is unique.
·Create a list of at least five user names and name it current_users.
·Create a list of five user names and name it new_users, and make sure one or two of them are also included in the list current_ In users.
·Traverse list new_users, for each user name, check whether it has been used. If so, print a message indicating that another user name needs to be entered; Otherwise, a message is printed indicating that the user name is not used.
·Ensure that the comparison is case insensitive; In other words, if the user name 'John' is already in use, the user name 'John' should be rejected.
current_users=['xuyan','yuhangli','jiayanghou','Jiachenyang','baoyusong','admin'] new_users=['jiachenyang','Baoyusong','lili','shasha','xinxin'] #Convert to lowercase comparison lower_new_users= [users.lower() for users in new_users] lower_current_users=[users.lower() for users in current_users] for user in lower_new_users: if user in lower_current_users: print('sorry,'+user+' have been used,you need to input another name.') else: print(user+' is not used.')
(there is simpler code here, but I won't 😥. And the used name of the feedback is not the original name entered by the user, but lowercase)
5-11 # ordinal number: ordinal number indicates position, such as 1st and 2nd. Most ordinals end with th, with the exception of 1, 2, and 3.
·The numbers 1 to 9 are stored in a list.
·Traverse the list.
·An if elif else structure is used in the loop to print the ordinal number corresponding to each number. The output content should be 1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th and 9th, but each ordinal number is exclusive.
figure=range(1,10) for number in figure: if number==1: print('1st') elif number==2: print('2nd') elif number==3: print('3rd') else: print(str(number)+'th')