#5-5 def judge(alien_color): if alien_color=='green': print('You have earned 5 points!') if alien_color=='yellow': print('You have earned 10 points!') if alien_color=='red': print('You have earned 15 points!') alien_color='green' judge(alien_color) alien_color='yellow' judge(alien_color) alien_color='red' judge(alien_color) print('') #5-9 name_list={'admin','tom','ann','john','caroline'} if name_list: for name in name_list: if name=='admin': print("Hello admin, would you like to see a status report?") else: print("Hello "+name.title()+", thank you for logging in again") else: print('We need to find some users!') name_list.clear() print('') #5-10 current_users={'mary','tony','rose','molly','hetty'} new_users={'john','tony','hetty','peter','jenny'} for new_user in new_users: if new_user.lower() in current_users: print("This username is already been used, please use another one.") else: print("This name is't been used.") print('') #5-11 num_list=range(1,10) for num in num_list: if(num==1): print("1st") elif(num==2): print("2nd") elif(num==3): print("3rd") else: print(str(num)+'th') print('') #6-1 friend={'first_name':'shi','last_name':'tian','age':19,'city':'guangzhou'} print("My friend's first name is "+friend['first_name']+'.') print("My friend's last name is "+friend['last_name']+'.') print("My friend's age is "+str(friend['age'])+'.') print("My friend lives in "+friend['city']+'.\n') #6-5 rivers={'nile':'egypt','the yellow river':'china','the amazon river':'the north america'} for river,country in rivers.items(): print(river.title()+' run through '+country.title()) for river in rivers.keys(): print(river.title()) for country in rivers.values(): print(country.title()) #6-8 pets={ 'cat':{ 'kind':'Scottish Fold', 'owner':'Kitty', }, 'dog':{ 'kind':'Alaska', 'owner':'QLY', }, } for pet,pet_info in pets.items(): print('The cute '+pet+' '+pet_info['kind']+' belongs to '+pet_info['owner']+'.\n') #6-9 favorite_places={'QLY':{'Shenzhen','Hongkong'},'Kate':{'Beijing','Shanghai','Guangzhou'},'Ken':{'New York'}} for person,places in favorite_places.items(): print(person+"'s favorite places:") for place in places: print(place)
上述程序输出如下:
You have earned 5 points! You have earned 10 points! You have earned 15 points! Hello John, thank you for logging in again Hello Ann, thank you for logging in again Hello admin, would you like to see a status report? Hello Caroline, thank you for logging in again Hello Tom, thank you for logging in again This name is't been used. This name is't been used. This name is't been used. This username is already been used, please use another one. This username is already been used, please use another one. 1st 2nd 3rd 4th 5th 6th 7th 8th 9th My friend's first name is shi. My friend's last name is tian. My friend's age is 19. My friend lives in guangzhou. Nile run through Egypt The Yellow River run through China The Amazon River run through The North America Nile The Yellow River The Amazon River Egypt China The North America The cute cat Scottish Fold belongs to Kitty. The cute dog Alaska belongs to QLY. QLY's favorite places: Shenzhen Hongkong Kate's favorite places: Beijing Guangzhou Shanghai Ken's favorite places: New York