Python基础练习题目1
1.利用range函数生成10以内(不包含10)数字,并用for循环依次输出。
for i in range(1, 10):
print(i)
2.有players = [‘charles’,‘martina’,‘michael’,'florence,‘eli’]列表,请将列表中元素的首字母大写后依次输出。
players = ['charles','martina','michael','florence','eli']
for item in players:
print(item.capitalize())
3.有列表cars = [‘audi’, ‘bmw’, ‘subaru’, ‘toyota’]。遍历该列表,输出各个元素,要求将‘bmw’所有首字母大写。其余首字母大写。
cars = ['audi', 'bmw', 'subaru', 'toyota']
for item in cars:
if item == 'bmw':
print(item.upper())
else:
print(item.capitalize())
4.有available_toppings = [‘mushrooms’, ‘olives’, ‘green peppers’, ‘pepperoni’, ‘pineapple’, ‘extra cheese’] 与requested_toppings = [‘mushrooms’, ‘french fries’, ‘extra cheese’]两个列表,请判断requested_toppings中的元素是否在avaialbe_toppings列表中,如果存在,输出’yes’,否则输出’no’。
available_toppings = ['mushrooms', 'olives', 'green peppers', 'pepperoni', 'pineapple', 'extra cheese']
requested_toppings = ['mushrooms', 'french fries', 'extra cheese']
for item in requested_toppings:
if item in available_toppings:
print('yes')
else:
print('no')
5.在星际大战游戏中,外星飞机水平移动距离与其速度类型直接相关其中,'slow’类型,飞机x轴每秒移动1个距离,'medium’类型,飞机x轴每秒移动2个距离,'fast’类型,飞机x轴每秒移动3个距离。请计算飞机alien_0 = {‘x_position’:0,‘y_position’:25, ‘speed’:‘medium’}飞行3秒后的位置,并对其位置进行修改,并输出新的横纵坐标位置。
alien_0 = {
'x_position':0,'y_position':25, 'speed':'medium'}
def check_x(alien_0):
if alien_0['speed'] == 'slow':
alien_0['x_position'] += 3
elif alien_0[