Python_day08_homework

# #1、有列表['alex',49,[1900,3,18]],分别取出列表中的名字,年龄,出生的年,月,日赋值给不同的变量

l = ['alex', 49, [1900, 3, 18]]
name = l[0]
age = l[1]
year = l[2][0]
month = l[2][1]
day = l[2][2]



# 2、用列表的insert与pop方法模拟队列

l=[]
l.append('first')
l.append('second')
l.append('third')
print(l)
print(l.pop(0))
print(l.pop(0))
print(l.pop(0))



# 3. 用列表的insert与pop方法模拟堆栈

l=[]
l.append('first')
l.append('second')
l.append('third')
print(l)
print(l.pop())
print(l.pop())
print(l.pop())



# 4、简单购物车,要求如下:
# 实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,购买个数以三元组形式加入购物列表,如果输入为空或其他非法输入则要求用户重新输入  

msg_dic={
'apple':10,
'tesla':100000,
'mac':3000,
'lenovo':30000,
'chicken':10,
}
for key,value in msg_dic.items():
print("商品名:{} 商品价格:{} ".format(key, value))
shop_list = []
while True:
good_name = input("请输入商品名:").strip()
if good_name not in msg_dic:
print("商品名输入错误,请重新输入!")
continue
good_price = msg_dic[good_name]
while True:
good_count = input("请输入购买个数:").strip()
if good_count.isdigit():
break
print("购买数量输入非法!请重新输入!")
good_dict = {
"商品名" : good_name,
"商品单价" : good_price,
"商品数量" : good_count
}
shop_list.append(good_dict)
print(shop_list)




# 5、有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中
# 即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}

l0 = [11,22,33,44,55,66,77,88,99,90,100]
l1 =[]
l2 =[]
for i in l0:
if i < 66:
l1.append(i)
else:
l2.append(i)
d = {
"k1":l1,
"k2":l2
}
print(d)



# 6、统计s='hello alex alex say hello sb sb'中每个单词的个数

s='hello alex alex say hello sb sb'
count = 1
i = 0
while i < len(s)-2:
while s[i] != ' ':
i += 1
if s[i] == ' ':
count += 1
i += 1
print(count)



猜你喜欢

转载自www.cnblogs.com/Kevin-x/p/12465082.html