python编程实战3

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.

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值