昨天圣诞给自己放了个假,今天继续!
if-else语句和if-elif-if语句
同C,只不过python把“else if”缩写成了“elif”。
使用if语句来处理列表
检查特殊元素
书上的例子
一家比萨店在制作比萨时,每添加一种配料都打印一条消息。通过创建一个列表,在其中包含顾客点的配料,并使用一个循环来指出添加到比萨中的配料,能以极高的效率编写这样的代码。然而比萨店的青椒用完了,该如何处理呢?为妥善处理这种情况,可在for循环中包含一条if语句:
requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
for requested_topping in requested_toppings:
if requested_topping == 'green peppers':
print("Sorry, we are out of green peppers right now")
else:
print(f"Adding {requested_topping}.")
print("\nFinished making your pizza!")
确定列表不是空的
Python将在列表至少包含一个元素时返回True,并在列表为空时返回False。
requested_toppings = []
if requested_toppings:
for requested_topping in requested_toppings:
print(f"Adding {requested_topping}.")
print("\nFinished making your pizza!")
else:
print("Are you sure you want a plain pizza?")
使用多个列表:
顾客的要求往往五花八门,在比萨配料方面尤其如此。如果顾客要在比萨中要求炸薯条,该怎么办呢?可使用列表和if语句来确定能否满足顾客的要求。
available_toppings = ['mashroom', 'olives', 'green peppers',
'pepperoni', 'pineapple', 'extra cheese']
requested_toppings = ['mushroom', 'french fries', 'extra cheese']
for requested_topping in requested_toppings:
if requested_topping in available_toppings:
print(f"Adding {requested_topping}.")
else:
print(f"Sorry, we don't have {requested_topping}.")
print("\nFinished making your pizza!")
练习
按下面的说明编写一个程序,模拟网站如何确保每位用户的用户名都独一无二。
创建一个至少包含5个用户名的列表,并将其命名为current_users。
在创建一个包含5个用户名的列表,将其命名为new_users,并确保其中有一两个用户名也包含在列表current_users中。
遍历列表new_users,对于其中的每个用户名,都检查它是否已被使用。如果是,就打印一条信息,指出需要输入别的用户名;否则,打印一条信息,指出这个用户名未被使用。
确保比较时不区分大小写。换句话说,如果用户名“John”已被使用,应拒绝用户名“JOHN”。
current_users = ['Beluga', 'Hecker', 'Skittle',
'Sweeite', 'Pablo', 'Lester', 'Ralph', 'Pepper']
new_users = ['Beluga', 'Loyce', 'Moriarty',
'Logic', 'Sweeite']
CURRENT_USERS = []
for current_user in current_users:
CURRENT_USERS.append(current_user.upper())
for new_user in new_users:
if new_user.upper() in CURRENT_USERS:
print(f"Sorry, the name '{new_user}' has been used. Please choose another name.")
else:
print(f"The name '{new_user}' is available.")
字典
这就是一个简单的字典:
alien_0 = {'color': 'green', 'points': 5}
print(alien_0['color'])
print(alien_0['points'])
在Python中,字典是一系列键值对。每个键都与一个值相关联,你可以使用键来访问相关联的值。
添加键值对
字典是一种动态结构,可随时在其中添加键值对。
要添加键值对,可依次指定字典名、用方括号括起的键和相关联的值。就像这样:
alien_0 = {'color': 'green', 'point': 5}
print(alien_0)
alien_0['x_position'] = 0
alien_0['y_position'] = 25
print(alien_0)
修改字典中的值
要修改字典中的值,可依次指定字典名、用方括号括起的键,以及与该键相关联的新值。 就像这样:
alien_0 = {'color': 'green', 'point': 5}
print(f"The alien is {alien_0['color']}.")
alien_0['color'] = 'yellow'
print(f"The alien is now {alien_0['color']}.")
删除键值对
alien_0 = {'color': 'green', 'point': 5}
print(alien_0)
del alien_0['point']
print(alien_0)
使用get()来访问值
如果你要求获取外星人的速度,而这个外星人没有速度,结果将是:
alien_0 = {'color': 'green', 'point': 5}
print(alien_0['speed'])
Python会显示“KeyError”,即存在键值错误。
就字典而言,可以使用方法get()在指定的键不存在时返回一个默认值,从而避免这样的错误。
方法get()的第一个参数用于指定键,是必不可少的;第二个参数为指定的键不存在时要返回的值,是可选的,若没有输入,则默认返回None。
alien_0 = {'color': 'green', 'point': 5}
speed_value = alien_0.get('speed', 'No speed value assigned.')
print(speed_value)
明天见,晚安!