python购物车程序简单代码

时间:2022-11-25 14:18:01

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

代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'''''
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购物车程序简单代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_38626799/article/details/77836527