python购物车程序简单代码

yipeiwu_com5年前Python基础

本文实例为大家分享了python购物车程序的具体代码,供大家参考,具体内容如下

代码:

''''' 
Created on 2017年9月4日 
 
@author: len 
''' 
 
 
product_list = [ 
 ('Robot',200000), 
 ('MacPro',12000), 
 ('Iphone8',8888), 
 ('Hello World',1200), 
    ] 
shopping_list = [] 
user_salary=input("请输入你的工资:") 
if user_salary.isdigit(): 
 user_salary = int(user_salary) 
 while True: 
  print("商品如下:") 
  for index,item in enumerate(product_list): 
    
   print (index,item) 
  user_choice = input("请输入要购买的商品编号:") 
  if user_choice.isdigit(): 
   user_choice = int(user_choice) 
   if user_choice < len(product_list) and user_choice > -1: 
    p_item = product_list[user_choice] 
    if user_salary>=p_item[1]: 
     shopping_list.append(p_item) 
     user_salary-=p_item[1] 
     print("购买商品",p_item,"成功您的余额为",user_salary,"元!" ) 
    else: 
     print("您的余额为",user_salary,"余额不足以购买此商品,购买失败!") 
        
   else: 
    print("并无此产品!") 
  elif user_choice == "q": 
   print("--------shopping list-------") 
   for i in shopping_list: 
    print(i) 
   exit() 
  else: 
   print("invalidate!!!")

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python中出现IndentationError:unindent does not match any outer indentation level错误的解决方法

Python中出现IndentationError:unindent does not match any outer indentation level错误的解决方法

今天在网上copy的一段代码,代码很简单,每行看起来该缩进的都缩进了,运行的时候出现了如下错误:  【解决过程】  1.对于此错误,最常见的原因是,的确没有缩进。...

celery4+django2定时任务的实现代码

网上有很多celery + django实现定时任务的教程,不过它们大多数是基于djcelery + celery3的; 或者是使用django_celery_beat配置较为繁琐的。...

进一步理解Python中的函数编程

我们最好从最难的问题开始:“到底什么是函数编程 (FP)?”一个答案可能会说 FP 就是您在使用例如 Lisp、Scheme、Haskell、ML、OCAML、Clean、Mercury...

Centos下实现安装Python3.6和Python2共存

写在前面 centos6.8中默认自带的python版本为python2.6,那么这里需要将其改为python3 下载并解压 官方下载地址为 https://www.python.o...

Python list与NumPy array 区分详解

1. 数据类型 type() #!/usr/bin/env python # -*- coding: utf-8 -*- # Yongqiang Cheng from __fut...