python字符串实践

python字符串实践

收银小票打印尝试

python字符串对齐方式

首先,看几个命令的效果:

1
2
3
4
5
6
7
8
from math import pi


print("{0:<10.2f}\n{0:^10.2f}\n{0:>10.2f}".format(pi))
print('{}'.format(pi))
print('{0}'.format(pi))
print('{{:{}}}'.format(10))
print('{:10.2f}'.format(pi))

运行结果:

1
2
3
4
5
6
7
3.14      
3.14
3.14
3.141592653589793
3.141592653589793
{:10}
3.14

收银小票尝试:

实现方式有点low,写的有点乱。。

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
#!/usr/bin/env python3
# -*-coding: utf-8 -*-
"""
@author: kyle
@time: 2019/6/20 16:06
"""
# from math import pi

# print("{0:<10.2f}\n{0:^10.2f}\n{0:>10.2f}".format(pi))
# print('{}'.format(pi))
# print('{0}'.format(pi))
# print('{{:{}}}'.format(10))
# print('{:10.2f}'.format(pi))

def fruit_price(fruit):
if fruit == 'Watermelon':
return 1.6
elif fruit == 'Apple':
return 10
elif fruit == 'Peach':
return 3.3
elif fruit == 'Pear':
return 2
else:
return None

def enter_price():
price = []
weights = []
fruits = []
num = 0
while True:
num += 1
fruit = input('Enter the fruit: ')
weight = input('Enter the weight: ')
fruits.append(fruit)
weights.append(weight)
a = fruit_price(fruit)
price.append(a)
if fruit == 'q' or weight == 'q':
break
num -= 1
fruits.remove(fruits[-1])
price.remove(price[-1])
weights.remove(weights[-1])

return [num,fruits,weights,price]


fruit_res = enter_price()
print(fruit_res)

# 设置小票宽度
width = int(input('Please enter your width: '))
price_width = 10
num_width = 10
item_width = width - price_width - num_width

# 设置小票表头格式
header_fmt = '{{:{}}}{{:^{}}}{{:>{}}}'.format(item_width, num_width, price_width)
fmt = '{{:{}}}{{:^{}}}{{:>{}.2f}}'.format(item_width, num_width, price_width)

print('*'* width)
print("Here's Your Ticket")

print('='* width)
print(header_fmt.format('Item', 'Num', 'Price'))
print('-' * width)

# 循环打印水果,单价,数量
num = fruit_res[0]
i = 0
prices = []
for i in range(int(num)):
prices.append(int(fruit_res[2][i]) * float(fruit_res[3][i]))
print(fmt.format(fruit_res[1][i], fruit_res[2][i], fruit_res[3][i]))
i += 1

print('-' * width)

# 打印总价
Sum = sum(prices)
item2_width = width - price_width
foot_fmt = '{{:<{}}}{{:>{}.2f}}'.format(item2_width, price_width)
print(foot_fmt.format('Summary', Sum))
print('-' * width)

运行结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Enter the fruit: Watermelon
Enter the weight: 10
Enter the fruit: Apple
Enter the weight: 20
Enter the fruit: Peach
Enter the weight: 30
Enter the fruit: q
Enter the weight: q
[3, ['Watermelon', 'Apple', 'Peach'], ['10', '20', '30'], [1.6, 10, 3.3]]
Please enter your width: 45
*********************************************
Here's Your Ticket
=============================================
Item Num Price
---------------------------------------------
Watermelon 10 1.60
Apple 20 10.00
Peach 30 3.30
---------------------------------------------
Summary 315.00
---------------------------------------------

Process finished with exit code 0

有空再改进。。

文章目录
  1. python字符串对齐方式
  2. 收银小票尝试:
|