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
|
#!/usr/bin/env python
import
sys
market = [
[
'Xiaomi Phone'
,
2400
],
[
'Iphone'
,
6000
],
[
'Computer'
,
3800
],
[
'Ipad'
,
1800
],
[
'Core Python'
,
69
],
[
'Router'
,
109
]
]
shop_list = [] #The shopping list of the buyer.
while
True:
salary = raw_input(
'Please input your salary per month:'
).strip()
if
not salary.isdigit():
print
'Please enter your salary.'
continue
salary =
int
(salary)
print
"Welcome to our market!You can buy something cool here, or you can enter 'quit' to left.\n"
break
#Upon:input the salary per month.输入工资同时避免异常情况。
while
True:
print
'The goods we serve are as follow:'
for
goods
in
market:
print market.index(goods),goods[
0
],goods[
1
]
#Upon:print the goods list.
choice = raw_input(
'What do you want to buy?'
)
if
choice ==
'quit'
: #
'quit'
system
print
'\nYour shopping list are as follow:'
for
goods
in
shop_list:
print goods[
0
],goods[
1
]
print
'Now you have %d left.'
% (salary)
sys.exit(
'Goodbye!'
)
elif len(choice) ==
0
: #
'enter'
system
continue
#实际上
'luanma'
system和
'enter'
system可以只用前者进行替代,这里只是想输出不同提示而已。
(即输入乱码时会提示,输入enter时不进行提示。)
if
not choice.isdigit(): #
'luanma'
system,即如果用户输入非数字时,会有提示
print
'Please input the right choice.(Number to buy things and quit to quit.)\n'
continue
#Upon:
'quit'
system ,
'enter'
system and
'luanma'
handle system.
choice =
int
(choice)
if
choice >= len(market):
print
'Could not find the item, try again!\n'
continue
pri = market[choice]
#Upon:To check
if
the number
is
legal.确认输入的数字是否在合法范围内
pri = market[choice]
if
pri[
1
] <= salary:
salary = salary - pri[
1
] #The remaining money.
shop_list.append(pri) #Adding the goods to the list.
print
'You have added %s to your shopping list, and you have %d left.\n'
% (pri[
0
], salary)
else
:
print
''
'You have %d left.
You can not afford to buy %s, but you can
try
to buy other things.\n
''
' % (salary, pri[
0
])
|
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
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day2$ python myshop.py
Please input your salary per month:
Please enter your salary. ===>直接输入
'Enter'
时会有提示
Please input your salary per month:klkdf
Please enter your salary. ===>输入乱码时也会有提示
Please input your salary per month:
10000
Welcome to our market!You can buy something cool here, or you can enter
'quit'
to left.
The goods we serve are
as
follow: ===>先打印了一个商品菜单
0
Xiaomi Phone
2400
1
Iphone
6000
2
Computer
3800
3
Ipad
1800
4
Core Python
69
5
Router
109
What
do
you want to buy?
0
===>正确输入商品索引号,会提示当前用户购物信息
You have added Xiaomi Phone to your shopping list, and you have
7600
left.
The goods we serve are
as
follow:
0
Xiaomi Phone
2400
1
Iphone
6000
2
Computer
3800
3
Ipad
1800
4
Core Python
69
5
Router
109
What
do
you want to buy? ===>只输入
'Enter'
时,不会有提示输出,但会再次打印商品菜单
The goods we serve are
as
follow:
0
Xiaomi Phone
2400
1
Iphone
6000
2
Computer
3800
3
Ipad
1800
4
Core Python
69
5
Router
109
What
do
you want to buy?
3
You have added Ipad to your shopping list, and you have
5800
left.
The goods we serve are
as
follow:
0
Xiaomi Phone
2400
1
Iphone
6000
2
Computer
3800
3
Ipad
1800
4
Core Python
69
5
Router
109
What
do
you want to buy?eqwer ===>输入乱码时,会有提示,并接受再次输入
Please input the right choice.(
Number
to buy things and quit to quit.)
The goods we serve are
as
follow:
0
Xiaomi Phone
2400
1
Iphone
6000
2
Computer
3800
3
Ipad
1800
4
Core Python
69
5
Router
109
What
do
you want to buy?
9
===>输入不在合法范围内的数字时,也会有提示
Could not find the item,
try
again!
The goods we serve are
as
follow:
0
Xiaomi Phone
2400
1
Iphone
6000
2
Computer
3800
3
Ipad
1800
4
Core Python
69
5
Router
109
What
do
you want to buy?quit ===>正常退出,会打印用户总的购物清单信息
Your shopping list are
as
follow:
Xiaomi Phone
2400
Ipad
1800
Now you have
5800
left.
Goodbye!
|