一、购物车小程序第一版

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Hiuhung Wan
product_list = [
("iphone 8", 5888),
("Mi Note3", 2499),
("Bike", 788),
("Phthon 3.5", 59),
("Mac pro", 9888),
("Honor 6", 599)
]
notfalse = not False
shopping_cart = []
while notfalse:
salary = input("Input your salary:")
if salary.isdigit():
salary = int(salary) while True:
# for i in product_list:
# print(product_list.index(i),i)
for index, i in enumerate(product_list):
print(index, i) # 打印商品列表
user_choice = input("Please choose what you need to buy:")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice >= 0: # 用户选择了对应的商品
if product_list[user_choice][1] <= salary: # 买得起
salary = salary - product_list[user_choice][1] # 扣钱
shopping_cart.append(product_list[user_choice])
print("You have purchased %s, and your current balance is %s." % (
product_list[user_choice], salary))
elif user_choice == "": # 直接按的回车,没有输入
print("Please enter the corresponding number of the goods!")
else: # 钱不够,买不起!
print("Your balance is not enough!")
else: # 用户输入的数字超出商品总数
print("Please check the commodity number!")
elif user_choice == "q" or user_choice == "Q": # 要退出
print("------------shopping list-------------")
for index, i in enumerate(shopping_cart):
print(index, i)
print("Your current balance is %s." % salary)
exit()
else: # 用户输入的不是数字
print("Please enter the corresponding number of the goods!") elif salary == "q" or salary == "Q": # 用户想退出
notfalse = False
else: # 用户输入了其他字符
print("Please input your salary by number!")

  

Day2:购物车小程序的更多相关文章

  1. Python之路 day2 购物车小程序1

    #Author:ersa ''' 程序:购物车程序 需求: 启动程序后,让用户输入工资,然后打印商品列表 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 可随时 ...

  2. python 购物车小程序

    python 购物车小程序 功能要求:1.启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表2.允许用户根据商品编号购买商品3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒4. ...

  3. [作业] Python入门基础---购物车小程序

    1.购物车小程序: 1.1用户输入工资取60% 1.2打印输出商品菜单 1.3由用户输入数字选择 #__author:Mifen #date: 2018/11/27 # 购物车程序 #把工资作为账户的 ...

  4. python3 购物车小程序,余额写入文件保存

    python3 购物车小程序,余额写入文件保存 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan goods = ( ...

  5. python编写购物车小程序

     #练习#程序购物车#启动程序后,让用户输入工资,  然后打印商品列表,允许用户根据商品编号购买商品用户选择商品后 #检测余额是否够,够就直接扣款,不够就提醒可随时退出,退出时,打印已购买商品和余额  ...

  6. python写购物车小程序

    #!/usr/bin/env python3 # -*- coding:utf-8 -*- # @Author: Skyell Wang # @Time : 2018/5/22 15:50 # 基础要 ...

  7. python 练习购物车小程序

    # -*- coding:utf-8 -*- shp = [ ['iphone',5000], ['offee',35], ['shoes',800] ] pric_list = [] e = int ...

  8. 购物车小程序(while循环,列表)

    while True: salary = input("please input your salary:") if salary.isdigit(): salary=int (s ...

  9. python 购物车小程序(列表、循环、条件语句)

    goods = [ ['iphone6s', 5800], ['mac book', 9000], ['coffee', 32], ['python book', 80], ['bicyle', 15 ...

随机推荐

  1. hdu4565---So Easy!(矩阵)

    Problem Description A sequence Sn is defined as: Where a, b, n, m are positive integers.┌x┐is the ce ...

  2. 关于router-link的传参以及参数的传递

    1.路径:http://localhost:8081/#/test?name=1 <router-link :to="{path:'/test',query: {name: id}}& ...

  3. HTML5入门:HTML5的文档声明和基本代码

    HTML5的文档声明: HTML5的文档声明,不同于HTML4.0和XHTML,它精简了许多代码,只保留<!DOCTYPE html>开头,必须位于HTML5文档的第一行,它可以用来告诉浏 ...

  4. js中常用的对象—Array的属性和方法

    今天说一下,js中常用的内置对象——Array对象 Array常用属性: length prototype :给系统对象添加属性和方法 Array常用方法: Array.prototype.sum = ...

  5. 10款最好的Python IDE

    Python 的学习过程少不了集成开发环境(IDE)或者代码编辑器.这些 Python 开发工具帮助开发者加快使用 Python 开发的速度,提高效率.高效的代码编辑器或者 IDE 应该会提供插件,工 ...

  6. 最大子段和 模板题 51Nod 1049

    N个整数组成的序列a[1],a[2],a[3],…,a[n],求该序列如a[i]+a[i+1]+…+a[j]的连续子段和的最大值.当所给的整数均为负数时和为0. 例如:-2,11,-4,13,-5,- ...

  7. 2017国家集训队作业[agc016e]Poor Turkey

    2017国家集训队作业[agc016e]Poor Turkey 题意: 一开始有\(N\)只鸡是活着的,有\(M\)个时刻,每个时刻有两个数\(X_i,Y_i\),表示在第\(i\)个时刻在\(X_i ...

  8. netty实现TCP长连接

    所用jar包 netty-all-4.1.30.Final.jar 密码:rzwe NettyConfig.java,存放连接的客户端 import io.netty.channel.group.Ch ...

  9. XML学习总结(2)——XML简单介绍

    一.XML概念 Extensible Markup Language,翻译过来为可扩展标记语言.Xml技术是w3c组织发布的,目前推荐遵循的是W3C组织于2000发布的XML1.0规范. 二.学习XM ...

  10. XML学习总结(1)——XML入门

    一.XML语法学习 学习XML语法的目的就是编写XML 一个XML文件分为如下几部分内容: 文档声明 元素 属性 注释 CDATA区 .特殊字符 处理指令(processing instruction ...