如何利用Python实现购物程序思路以及实现代码


当前第2页 返回上一页

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

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

# 简单的购物小程序

 

product_list = [

  ['surface pro 4', 7800],

  ['dell xps 15', 12000],

  ['macbook', 12000],

  ['小米6', 2499],

  ['iphone7', 4600],

  ['坚果Pro', 1499]

]

shopping_list = []

 

 

# 判断输入的薪水格式是否正确

while True:

  salary = input('\n请输入您的薪水:')

  if not salary.isdigit():          # 薪水不是数字,结束循环

    print('\n输入格式有误!请重新输入...')

    continue

  break

 

 

balance = salary = int(salary)

 

print('\n-----------欢迎购买------------\n')

 

# 生成带序号的商品列表

for index, item in enumerate(product_list):

  print(index, item)

 

 

# 判断输入的序号是否符合要求

while True:

 

  while True:

    i = input('\n输入您要购买的商品序号,或输入 q 取消购买:')

    if i == 'q':                 # 输入 q 退出购买界面

      while True:

        a = input('\n是否继续购买?(Y/N):')

        if a != 'n' and a != 'N' and a != 'y' and a != 'Y':

          print('\n输入格式有误,请重试...')

          continue

        elif a == 'y' or a == 'Y':         # 继续购买

          break

        else:                    # 购买完毕

          if balance == salary:       # 没有买任何东西

            print('\n交易结束,购买失败...')

            exit()

          else:               # 结算 

            print('\n您已成功购买以下商品:\n')

            for item in shopping_list:

              print(item)

            print('\n共消费金额 %d 元,余额 %d 元' % (salary - balance, balance))

            exit()

      continue

 

    if not i.isdigit():             # 序号不是数字,结束循环

      print('\n输入格式有误!请重新输入...')

      continue

 

    i = int(i)

 

    if i < 0 or i >= len(product_list):  # 序号范围不正确,结束循环

      print('\n此商品不存在,请重新输入...')

      continue

    break

 

  product = product_list[i]

  price = int(product[1])

 

  # 判断余额是否充足,够就直接扣款,不够提醒

  if price <= balance:

    balance -= price

    shopping_list.append(product_list[i])

    print('\n您已成功购买 %s ,当前余额为 %d 元' %(product, balance))

  else:

    print('\n购买失败,您的余额不足...')

 

  while True:

    a = input('\n是否继续购买?(Y/N):')

    if a != 'n' and a != 'N' and a != 'y' and a != 'Y':

      print('\n输入格式有误,请重试...')

      continue

    break

 

  if a == 'Y' or a == 'y':

    continue

  else:

    break

 

if balance == salary:

  print('\n交易结束,购买失败...')

  exit()

else:

  print('\n您已成功购买以下商品:\n')

  for item in shopping_list:

    print(item)

  print('\n共消费金额 %d 元,余额 %d 元' %(salary-balance, balance))

  exit()

以上就是如何利用Python实现购物程序思路以及实现代码的详细内容,更多文章请关注木庄网络博客!!

返回前面的内容

相关阅读 >>

没学过编程可以自学Python

Python中字典操作的总结(六种)

Python在groupby分组后提取指定位置记录方法

Python中tornado安全cookie机制的讲解

Python中使用什么代替switch语句

Python中while和for的区别

开启Python取经之路-class-6(part 1)

Python实现校园网自动登录

Python针对给定列表中元素进行翻转操作的方法分析

什么是 Python

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...