Python 字典 while循环

# 创建一个简单的字典
alien_0 = {'color':'green','points':3}

# 字典访问
print(alien_0['color'])
print(alien_0['points'])

new_point = alien_0['points']
print("you just earned " + str( new_point) + ' points.')

#添加键-对值
print(alien_0)
alien_0['xPosition'] = 0
alien_0['yPosition'] = 5
print(alien_0)

green
3
you just earned 3 points.
{‘color’: ‘green’, ‘points’: 3}
{‘color’: ‘green’, ‘points’: 3, ‘xPosition’: 0, ‘yPosition’: 5}

# 创建一个空字典
alien_1 = {}
alien_1['color'] = 'yellow'
alien_1['points'] = 10
print(alien_1)

# 修改字典中的值
alien_1['color'] = 'red'
print("The alien is now " + alien_1['color'] + ".")

{‘color’: ‘yellow’, ‘points’: 10}
The alien is now red.

alien_0 = {'x_position':0,'y_position':5,'speed':'fast'}
print("Original position: " + str(alien_0['x_position']))

# 外星人向右移动
# 根据外星人的速度决定移动距离
if alien_0['speed'] == 'slow':
    x_increment = 1
elif alien_0['speed'] == 'medium':
    x_increment = 2
else:
    x_increment = 3
    
alien_0['x_position'] = alien_0['x_position'] + x_increment
print("New x_position: " + str(alien_0['x_position']))

Original position: 0
New x_position: 3

# 删除键-对值
alien_0 = {'color':'blue','points':10}
print(alien_0)
del alien_0['color']
print(alien_0)

{‘color’: ‘blue’, ‘points’: 10}
{‘points’: 10}

# 由类似对象组成的字典(如编程语言)
favorite_languages = {
    'jen':'python',
    'sarah':'c',
    'phil':'ruby',
    'edward':'java',
}

friends = ['phil','sarah']

for name in favorite_languages:
    print(name.title())
    
    if name in friends:
        print(" hi "+name.title()+
              ", I see your favorite language is "+
             favorite_languages[name].title()
             )
        
if 'erin' not in favorite_languages:
    print("Erin,plase take our pool")

Jen
Sarah
hi Sarah, I see your favorite language is C
Phil
hi Phil, I see your favorite language is Ruby
Edward
Erin,plase take our pool

#### 字典列表
# 生成一个空列表
alien_0 = []

# 生成100个外星人
for alien_number in range(100):
    new_alien = {'color':'red','points':10,'speed':'medium'}
    alien_0.append(new_alien)
    
for alien in alien_0[:5]:
    print(alien)
print("......")

# 显示一共生成了多少个外星人
print("Total number of aliens: "+ str(len(alien_0)))

{‘color’: ‘red’, ‘points’: 10, ‘speed’: ‘medium’}
{‘color’: ‘red’, ‘points’: 10, ‘speed’: ‘medium’}
{‘color’: ‘red’, ‘points’: 10, ‘speed’: ‘medium’}
{‘color’: ‘red’, ‘points’: 10, ‘speed’: ‘medium’}
{‘color’: ‘red’, ‘points’: 10, ‘speed’: ‘medium’}

Total number of aliens: 100

###字典中嵌套列表
pizza = {
    'crust':'thick',
    'topping':['mushrooms','extra cheese'],
}

print("you ordered a "+
     pizza['crust'] + "-crust pizza"+
     " with the following toppings:")

for topping in pizza['topping']:
    print("\t"+topping)

you ordered a thick-crust pizza with the following toppings:
mushrooms
extra cheese

### 字典中嵌套字典
users = {
    'aeinstein':{
        'first':'albert',
        'last':'einstein',
        'location':'princeton',
    },
    
    'mcurie':{
        'first':'marie',
        'last':'curie',
        'location':'paris',
    },
}

for user_name,user_info in users.items():
    print("\nUserame: " + user_name)
    full_name = user_info['first'] + user_info['last']
    location = user_info['location']
    
    print("\tFull name: " + full_name.title())
    print("\tlocation: "+ location.title())

Userame: aeinstein
Full name: Alberteinstein
location: Princeton

Userame: mcurie
Full name: Mariecurie
location: Paris

# 列表之间的移动
unconfirmed_users = ['alice','brian','candace']
confirmed_users = []

while unconfirmed_users:
    current_user = unconfirmed_users.pop()
    
    print("Verifying user: " + current_user.title())
    confirmed_users.append(current_user)

print("\nThe following users have been confirmed: ")
for confirmed_user in confirmed_users:
    print(confirmed_user.title())

Verifying user: Candace
Verifying user: Brian
Verifying user: Alice

The following users have been confirmed:
Candace
Brian
Alice

# 删除指定的所有元素
pets = ['dog','cat','fish','cat','bird','cat','dog']

while 'cat' in pets:
    pets.remove('cat')
    
print(pets)

[‘dog’, ‘fish’, ‘bird’, ‘dog’]

# 使用用户输入填充字典
responses = {}

active = True

while active:
    name = input("\nWhat is your name? ")
    response = input("Which mountain would you like to climb someday? ")
    
    responses[name] = response
    
    repeat = input("Would you like to let annother person response? (yes/no) ")
    if repeat == 'no':
        active = False
        
print("\n-----Poll Result-----")
for name,response in responses.items():
    print(name + "would like to climb "+ response + " .")

What is your name? Tom
Which mountain would you like to climb someday? Huashan
Would you like to let annother person response? (yes/no)yes

What is your name? Cindy
Which mountain would you like to climb someday? Taishan
Would you like to let annother person response? (yes/no)yes

What is your name? John
Which mountain would you like to climb someday? Xiangshan
Would you like to let annother person response? (yes/no)no

-----Poll Result-----
Tomwould like to climb Huashan .
Cindywould like to climb Taishan .
Johnwould like to climb Xiangshan .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jackson的生态模型

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值