您的位置:首页 > 编程语言 > Python开发

014--python运算符和作业改进

2017-04-16 16:44 211 查看

一、运算符

    % 取模,返回商的余数

    10/3     3.33333333335

    10//3    3   地板除,取整数,不是四舍五入

    a = 3  b = 5  ----->   a<b and a==4 or b<10 and a>1

    or前面的条件成立,则不走or后面的式子;or前面的条件不成立,则不考虑前面的式子,直接运算后面的

    '123'.isdigit() 判断一个字符串是否可以转换为数字

    身份判断:type(123) is  int    type('123') is  str

    位运算:

              128  64  32  16  8  4  2  1

   a=60       0    0  1   1    1  1  0  0

   b=13   0    0   0  0    1   1  0  1

 &按位与      0    0   0  0    1   1  0  0

 |按位或   0     0   1  1    1   1  0  1

二、作业

    三级菜单(low版)

shopping_cart = {} #购物车
product_list = [
['iphone',5800],
['小米手机',2000],
['茅台酒',650],
['笔',5],
['电饭锅',200]
]
salary = int(input('Input your salary:'))
while True:
index = 0
for product in product_list:
print(index,product)
index+=1
choice = input('请输入商品编号:')
if choice.isdigit():
choice = int(choice)
if choice>=0 and choice<=len(product_list):
product = product_list[choice]
if product[1] <= salary:
if product[0] in shopping_cart:
shopping_cart[product[1]]+=1
else:
shopping_cart[product[0]]=[product[1],1]
salary-=product[1]
print('购物车已添加:'+product[0]+' ,您的余额为:'+str(salary))
else:
print("工资不够,商品的价格为:%s ,还差:%s"%(str(product[1]),product[1]-salary))
else:
print('编号不存在,请重新输入')
elif choice=='q':
print('-----------您已购买以下商品-----------')
id_count = 1
total_cost = 0
print('id       商品      单价      数量      总价')
for key in shopping_cart:
print("%s\t\t%s\t\t%s\t\t%s\t\t%s"
%(id_count,
key,
shopping_cart[key][0],
shopping_cart[key][1],
shopping_cart[key][0]*shopping_cart[key][1]
)
)
id_count+=1
total_cost+=shopping_cart[key][0]*shopping_cart[key][1]
print('您的总花费为:%s'%total_cost)
print('您的余额为:%s'%salary)
print('----------------end---------------')
else:
print('编号输入错误')
View Code

 

  

    

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: