Python学习3
猜数游戏
import random
while True:
first=random.randrange(100)
while True:
second=int(input('请猜'))
if first>second:
print('小了')
elif first==second:
print('对了')
break
else:
print('大了')
购物车
flag = 'ok'
lists = []
goods_lists = [['水果',[['鸭梨',18],['桃子',30],['苹果',21],['榴莲',5],['香蕉',3]]],['蔬菜',[['胡萝卜',11],['香菜',21],['圆白菜',3],['土豆',2],['地瓜',4]]]]
while True:
if flag=='no':
total_price = 0
print('商品名称 单价 购买数量 购买总价')
for a, b, c, d in lists:
print(' ' + a, ' ' * 3 + str(b), ' ' * 3 + str(c), ' ' * 3 + str(d))
total_price += d
print('总价格为' + str(total_price))
break
n = 1
print('欢迎光临 XXX超市')
print('################################')
for a,b in goods_lists:
print(' '+str(n)+' '+a)
n = n+1
print('################################')
num1 = int(input('请选择分类序号:')) - 1
while True:
m = 1
print('欢迎光临 XXX超市')
print('################################')
for goods in goods_lists[num1][1]:
print(' '+str(m)+' '+goods[0]+' '+str(goods[1])+'元')
m = m + 1
print('################################')
num2 = int(input('请选择商品序号:'))-1
if num2==-1:
break
if num2==-2:
flag = 'no'
break
n_lists = []
num3 = int(input('请选择购买数量(输入0返回上级菜单,输入-1结算退出)'))
n_lists.append(goods_lists[num1][1][num2][0])
n_lists.append(goods_lists[num1][1][num2][1])
n_lists.append(num3)
n_lists.append(goods_lists[num1][1][num2][1]*num3)
lists.append(n_lists)
我的
flag = 'ok'
lists = []
goods_lists = [['水果', [['苹果', 5], ['葡萄', 10], ['草莓', 15], ['芒果', 8], ['橘子', 6]]]
, ['蔬菜', [['胡萝卜', 2], ['土豆', 1], ['芹菜', 3], ['菠菜', 4], ['大葱', 1]]]]
while True:
if flag == 'no':
total_price = 0
print('商品名称 单价 数量 总价')
for a, b, c, d in lists:
print(' ' + a, ' ' * 3 + str(b), ' ' * 3 + str(c), ' ' * 3 + str(d))
total_price += d
print('总价为' + str(total_price))
break
n = 1
print('欢迎光临55超市')
print('########################')
for a, b in goods_lists:
print(' '+str(n)+' '+a)
n = n + 1
print('########################')
num1 = int(str(input('请选择分类序号:'))) - 1
while True:
m = 1
print('欢迎光临55超市')
print('########################')
for goods in goods_lists[num1][1]:
print(' '+str(m)+' '+goods[0]+' '+str(goods[1])+'元')
m = m + 1
print('########################')
num2 = int(str(input('请选择商品序号:'))) - 1
if num2 == -1:
break
if num2 == -2:
flag = 'no'
break
n_lists = []
num3 = int(str(input('请选择购买数量(输入0返回上级菜单,输入-1结算退出)')))
n_lists.append(goods_lists[num1][1][num2][0])
n_lists.append(goods_lists[num1][1][num2][1])
n_lists.append(num3)
n_lists.append(goods_lists[num1][1][num2][1] * num3)
lists.append(n_lists)