第三次作业(5-6章)

5-8  方式跟管理员打招呼 以特殊方式跟管理员打招呼:

names = ['Eric','admin','alice','ben','mircal']
for name in names:
    if name == 'admin':
        print("Hello admin, would you like to see a status report?")
    else:
        print("Hello "+ name +", thank you for logging in again")

测试代码:

Hello Eric, thank you for logging in again
Hello admin, would you like to see a status report?
Hello alice, thank you for logging in again
Hello ben, thank you for logging in again
Hello mircal, thank you for logging in again




5-9 有用户的情形 处理没有用户的情形:

names = ['Eric','admin','alice','ben','mircal']
while names:
    name = names.pop()
    if name == 'admin':
        print("Hello admin, would you like to see a status report?")
    else:
        print("Hello "+ name +", thank you for logging in again")
print("We need to find some users!")

测试代码:

Hello mircal, thank you for logging in again
Hello ben, thank you for logging in again
Hello alice, thank you for logging in again
Hello admin, would you like to see a status report?
Hello Eric, thank you for logging in again
We need to find some users!




5-10 检查用户名:

users = ['Eric','admin','alice','ben','mircal']
new_users = ['admin','mircal','jam','rical','nical']
'''admin and mircal is the same'''
for new_user in new_users:
    if new_user in users:
        print("The name of "+ new_user + " has been used!")
    else:
        print("The user name of " + new_user + " has not been used!")

测试代码:

The name of admin has been used!
The name of mircal has been used!
The user name of jam has not been used!
The user name of rical has not been used!
The user name of nical has not been used!




6-1 朋友的信息:

star = {'first_name':'Stephen','last_name':'Curry','age':30,'city':'Golden State'}
print(star)
{'first_name': 'Stephen', 'last_name': 'Curry', 'age': 30, 'city': 'Golden State'}



6-5 河流:

rivers = {'Yello river':'China','Nile':'Egypt','Die Donau':'Germany'}
for key,value in rivers.items():
    print("The " + key +" runs through "+ value + ".")
print("\n")
for key in rivers.keys():
    print(key)
print("\n")
for value in rivers.values():
    print(value)

测试代码:

The Yello river runs through China.
The Nile runs through Egypt.
The Die Donau runs through Germany.

Yello river
Nile
Die Donau

China
Egypt
Germany


6-8 宠物:

dag = {'owner':'Alice','type':'Husky'}
cat = {'owner':'Jam','type':'Cafe cat'}
horse = {'owner':'Ben','type':'bmw'}
pets = [dag,cat,horse]
for pet in pets:
    print(pet)

测试代码:

{'owner': 'Alice', 'type': 'Husky'}
{'owner': 'Jam', 'type': 'Cafe cat'}
{'owner': 'Ben', 'type': 'bmw'



6-9 喜欢的地方:

favorite_places = {'Alice':['Roma','Paris'],'Jam':['Tokyo','California','Roma'],'Amy':['Paris','Tokyo']}
for key,value in favorite_places.items():
    print(key)
    print(value)

测试代码:

Alice
['Roma', 'Paris']
Jam
['Tokyo', 'California', 'Roma']
Amy
['Paris', 'Tokyo']




6-11 城市:

cities = {'Roma':{'country':'Italy','population':'100,000','fact':'Beautiful'},
                   'Tokyo':{'country':'Japan','population':'200,000','fact':'Romantic'},
                   'Paris':{'country':'France','population':'300,000','fact':'crowded'}}

for key,value in cities.items():
    print(key)
    message = "Located in "+value['country'] + " with a population of " + value['population']+ ", and it's "+ value['fact']+'.'
    print(message)

测试代码:

Roma
Located in Italy with a population of 100,000, and it's Beautiful.
Tokyo
Located in Japan with a population of 200,000, and it's Romantic.
Paris
Located in France with a population of 300,000, and it's crowded.


猜你喜欢

转载自blog.csdn.net/qianjq3/article/details/79668639