继续死磕python
一、数据运算
算术运算
比较运算
赋值运算
逻辑运算
成员运算
身份运算
位运算
其中左右移运算是逻辑左右移即缺失位补0,而算数右移缺失补符号位(注意逻辑运算都是补码运算即都取补码再运算,然后结果也是补码,按照补码的规则反向可以找到原码)
正数的原码、反码、补码都一样
负数的反码是符号位不变取反,补码是原码的反码+1
运算符优先级
二、 if...else
基本格式: if 判断语句:
操作
else:
操作
三、循环
for i in range: 循环range内的元素
while 判断:
练习一、
优化购物车
1. 用户退出时打印商品列表时,按以下格式
-------您购买的商品如下------
id 商品 数量 单价 总价
1 iPhone 2 5800 11400
2 coffee 1 30 30
。。。
总计价格: 11430元
--------end -------------
good =['phone','shoes','clothes','headset','apple','computer',] price=[5000,200,150,2000,2,8000] money = input("please your wages") if money == 'out': print("You didn't buy anthing and thank for your shopping") exit() else: i=0 while i<len(good): print(i,good[i],price[i]) i+=1 num = 0 sc=[] money=int(money) key = 0 buy_money=0 while True: if key == 1: l=0 print('您购买的商品如下') print('商品','数量','单价','总价') while l < 6: buy_numl = sc.count(good[l]) if buy_numl ==0: l+=1 continue else: print(good[l],'',buy_numl,' ',price[l],' ',price[l]*buy_numl) buy_money=buy_money+price[l]*buy_numl l+=1 print("总计价格:",buy_money) print("剩余金钱:",money) exit() # j=0 # while j < len(sc): # print(sc[j]) # j += 1 # print('you have ',money,'left') # exit() else: buy = input("please choose your goods") if buy=="out": key = 1 continue elif buy.isdigit(): buy = int(buy) if buy<6 and buy >= 0: if money>=price[buy]: sc.insert(num,good[buy]) money=money-price[buy] num =num + 1 print(money) else: print("you don't have enough money") print(money) continue else: print("no this goods number") continue else : if buy in good: if money < price[good.index(buy)]: print("you don't enough money") print(money) continue else: money=money-price[good.index(buy)] sc.insert(num, buy) num =num+ 1 print(money) else: print("not this good") continue
练习二、
打印省、市、县三级菜单
可随时返回上一级
可随时退出程序
menu = { '北京':{ '海淀':{ '五道口':{ 'soho':{}, '网易':{}, 'google':{} }, '中关村':{ '爱奇艺':{}, '汽车之家':{}, 'youku':{}, }, '上地':{ '百度':{}, }, }, '昌平':{ '沙河':{ '老男孩':{}, '北航':{}, }, '天通苑':{}, '回龙观':{}, }, '朝阳':{}, '东城':{}, }, '上海':{ '闵行':{ "人民广场":{ '炸鸡店':{} } }, '闸北':{ '火车战':{ '携程':{} } }, '浦东':{}, }, '山东':{}, } while True: for key in menu: print(key) chioce_city=input('请输入城') if chioce_city in menu: if menu[chioce_city]: while True: for key in menu[chioce_city]: print(key) chioce_region=input("请输入区") if chioce_region in menu[chioce_city]: if menu[chioce_city][chioce_region]: while True: for key in menu[chioce_city][chioce_region]: print(key) chioce_town=input('请输入镇') if chioce_town in menu[chioce_city][chioce_region]: if menu[chioce_city][chioce_region][chioce_town]: while True: for key in menu[chioce_city][chioce_region][chioce_town]: print(key) chioce_company=input("请输入公司") if chioce_company in menu[chioce_city][chioce_region][chioce_town]: print(chioce_company,"真牛逼") elif chioce_company == 'b': break elif chioce_company == 'q': exit() else: print('傻Ⅹ,输错了吧') continue else: print("它太小了,不能进去了") elif chioce_town == 'b': break elif chioce_town == 'q': exit() else: print('傻Ⅹ,输错了吧') continue else: print("它太小了,不能进去了") elif chioce_region == 'b': break elif chioce_region == 'q': exit() else: print('傻Ⅹ,输错了吧') continue else: print("它太小了,不能进去了") elif chioce_city=='b' or chioce_city=='q': exit() else: print('傻Ⅹ,输错了吧') continue