要求
数组
一、启动程序后让用户输入余额,并打印商品列表
app
二、用户经过输入编号购买商品
ide
三、用户选择商品购买后,根据余额判断成功或者失败,给出对应提示
学习
四、能够随时退出,退出后打印帐号余额以及购买的商品列表spa
构思
code
一、首先,用户余额须要进行存储,用户购买的物品须要进行存储在数组中
orm
二、用户购买成功后,将购买的物品放入物品集合,并用总金额减去余额
ci
三、若是失败,给出失败提示,并打印余额
input
四、用户选择继续后,不管成功失败,均可以继续购买string
代码
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
|
# 用户输入工资
balance
=
int
(
input
(
"Please input balance:"
))
# 定义衣服的数组
clothes
=
[[
"pants"
,
100
],[
"T-shirt"
,
50
],[
"skirt"
,
20
]]
# 我的所得,包括金钱和获取的物品
haveGoods
=
[balance,[]]
flag
=
True
while
flag:
# 打印衣服列表
print
(
"The clothes list is as follows"
)
print
(
"______clothesList______"
)
i
=
1
;
for
c
in
clothes:
print
(
'The number:'
,i,
":"
,c)
i
+
=
1
# 用户输入商品编号
code
=
int
(
input
(
"Please choose the number:"
))
# 判断钱是否够用
if
clothes[code
-
1
][
1
] <
=
haveGoods[
0
]:
# 在本身的购物清单中加入已购物品
haveGoods[
1
].append(clothes[code
-
1
])
# 减去花费的金钱
haveGoods[
0
]
-
=
clothes[code
-
1
][
1
]
print
(
"You have successfully purchased!"
)
print
(
"Your account balance is:"
,haveGoods[
0
])
else
:
print
(
"Your account balance is insufficient!"
)
print
(
"Your account balance is:"
,haveGoods[
0
])
judge
=
input
(
"You can press any button to continue,or input 'n' to leave:"
)
if
judge
=
=
"n"
:
flag
=
False
print
(
"Your account balance is:"
,haveGoods[
0
])
print
(
"Your shopping list is as follows:"
)
print
(
"______clothesList______"
)
for
h
in
haveGoods[
1
]:
print
(h)
|
以上就是本文的所有内容,但愿对你们的学习有所帮助,也但愿你们多多支持脚本之家。