循环作业
# 要求:
根据当前的国家关税,还有人民币对应国家货币汇率 计算一下
把一件产品的价格得出来
1 因为input接受到的都是字符串
2 怎么处理数字呢?
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
shopping_cart = [] shopping_monry = [] # 中国进境商品及税率表 commodity_dict = { '0': { 'category': ['书包','拉杆箱','钱包','手提包'], 'monry': 500, 'tariff': 0.3 # 税率: 30% }, '1': { 'category': ['皮鞋','运动鞋','AJ','老头鞋'], 'monry': 1000, 'tariff': 0.3 # 税率: 30% }, '2': { 'category': ['手表','钟表','怀表','落地钟'], 'monry': 5000, 'tariff': 0.15 # 税率: 15% }, '3': { 'category': ['金首饰','银首饰','钻石','翡翠'], 'monry': 20000, 'tariff': 0.3 # 税率: 30% }, '4': { 'category': ['洗面奶','化妆水','洗发膏','面膜'], 'monry': 2000, 'tariff': 30 # 税率: 30% }, '5': { 'category': ['红酒','香槟酒','啤酒','白酒'], 'monry': 10000, 'tariff': 0.6 # 税率: 60% }, } # 人民币对应各国货币换算表 conversion_dict = { 'USD': 6.92, # 美元 'JPY': 0.06, # 日元 'KRW': 0.006, # 韩元 'EUR': 7.93, # 欧元 'HKD': 0.88, # 港元 'CAD': 0.99 # 加拿大元 } while True: choice = input(''' 你要去哪个国家购物? USD->美国 JPY->日本 KRW->韩国 EUR->欧洲 HKD->香港 CAD->加拿大 q->不买了\n>>: ''') if not choice:continue if choice not in conversion_dict: if choice == 'q': break continue while True: for key in commodity_dict: print(key,commodity_dict[key]['category']) shop_number = input('请输入商品类编号(输入q结账): ') if shop_number == 'q': total_monry = 0 for mon in shopping_monry: total_monry += mon total = total_monry * conversion_dict[choice] print('您本次在%s购买了: %s\n消费总金额为: %d人民币' % (choice,shopping_cart,total)) break elif shop_number: shop_num = int(shop_number) if shop_num < 0 and shop_num > 5: print('商品类不存在') print(commodity_dict[shop_number]['category']) shop_name = input('请输入要购买的商品: ') if shop_name: shopping_cart.append(shop_name) shop_monry = commodity_dict[shop_number]['monry'] + (commodity_dict[shop_number]['monry'] * commodity_dict[shop_number]['tariff']) shopping_monry.append(shop_monry)
# 要求:
完成一个简单的计算器的功能 ( 1 + 1 , 1 - 1 , 1 * 1 , 1 / 1 )
1 复杂的写法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/usr/bin/python # -*- coding: utf-8 -*- import sys while True: # 接收算表达式 exp = input('请输入运算表达式(输入Q退出): ') # 1 + 1 2 - 2 3 * 3 4 / 4 # 运算符格式化掉空白字符 exp_format = exp.replace(' ','') # 输入为空时继续输入 if not exp:continue # 输入为q或者Q时退出 if exp == 'q' or exp == 'Q': sys.exit() # 取表达式中运算符,赋值给空运算符 operater = '' for my_operater in ['+','-','*','/']: if my_operater in exp_format: operater = my_operater break # 取出表达式字符左右字符并转换为浮点类型 lift_sum = float(exp_format[:exp_format.index(operater)]) right_sum = float(exp_format[exp_format.index(operater)+1:]) # 判断运算符,并进行运算 if operater == '+': sum = (lift_sum + right_sum) elif operater == '-': sum = (lift_sum - right_sum) elif operater == '*': sum = (lift_sum * right_sum) elif operater == '/': sum = (lift_sum / right_sum) # 打印运算结果 print('%s = %.2f' % (exp_format,sum))
2 简单的写法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/usr/bin/python # -*- coding: utf-8 -*- while True: # 接收算表达式 exp = input('请输入运算表达式(输入Q退出): ') # 1 + 1 2 - 2 3 * 3 4 / 4 exp_format = exp.replace(' ','').strip() for my_str in exp_format: if my_str: if not exp_format[exp_format.index(my_str)+1:] or not exp_format[:exp_format.index(my_str)] \ or not exp_format[exp_format.index(my_str)+1:].isdigit() or not exp_format[:exp_format.index(my_str)].isdigit():continue if my_str == '+': print(float(exp_format[:exp_format.index(my_str)]) + float(exp_format[exp_format.index(my_str)+1:])) elif my_str == '-': print(float(exp_format[:exp_format.index(my_str)]) - float(exp_format[exp_format.index(my_str)+1:])) elif my_str == '*': print(float(exp_format[:exp_format.index(my_str)]) * float(exp_format[exp_format.index(my_str)+1:])) elif my_str == '/': print(float(exp_format[:exp_format.index(my_str)]) / float(exp_format[exp_format.index(my_str)+1:]))