概要
按理说,我们入门的第一个小程序都应该是Hello World。因为比较简单,我这也就不做过多的演示 了。
下面是我写的一个小程序。主要用于练习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
44
45
46
47
48
49
50
51
52
53
54
55
|
#!/usr/bin/env python # -*- coding: utf-8 -*- """ @author: 烽火 @license: Apache Licence @file: shopping.py @time: 6/16/17 10:05 AM """ goods = [ ( "IPhone" , 5800 ), ( "Watch" , 2000 ), ( "MacBook" , 12000 )] goods_cart = [] mybudget = input ( "请输入您的预算:" ) # 不考虑是小数的情况 while not mybudget.isdigit(): mybudget = input ( "输入有误,请重新输入您的预算:" ) mybudget = int (mybudget); while True : print ( "商品列表" .center( 50 , "-" )) print ( "编号" .center( 8 , " " ), "名称" .ljust( 30 , " " ), "价格" .ljust( 10 , " " )) for i in enumerate (goods): print ( str (i[ 0 ]).center( 10 , " " ), str (i[ 1 ][ 0 ]).ljust( 31 , " " ), str (i[ 1 ][ 1 ]).ljust( 10 , " " )) user_choose = input ( "请输入您的选择:" ) if user_choose.isdigit(): user_choose = int (user_choose) if user_choose > = 0 and user_choose < len (goods): if (mybudget - goods[user_choose][ 1 ]) > = 0 : goods_cart.append(goods[user_choose]) mybudget - = goods[user_choose][ 1 ] print ( "预算还有%d" % mybudget) else : print ( "预算不够啦~" ) else : print ( "不存在该商品~" ) elif user_choose = = 'q' : break else : print ( "您的输入有误~" ) print ( "预算还剩%d了" % (mybudget)) print ( "购物车商品信息" .center( 50 , "-" )) for i in enumerate (goods_cart): print ( str (i[ 0 ]).center( 10 , " " ), str (i[ 1 ][ 0 ]).ljust( 31 , " " ), str (i[ 1 ][ 1 ]).ljust( 10 , " " )) |
运行结果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/w695050167/article/details/73331811