脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python购物车程序简单代码

python购物车程序简单代码

2021-02-01 00:33不会代码的程序员 Python

这篇文章主要为大家详细介绍了python购物车程序的简单代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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

延伸 · 阅读

精彩推荐