我对Python编程比较陌生,使用Python 3.x,正在开发一个Barbershop p p.O.S系统,管理员有权添加服务和相应的价格。 我使用漂亮的表库来实现打印出一个带有serviceID、service和price的表。
这是我的代码:
from prettytable import PrettyTable
import random
serviceID = []
services = []
price = []
x = PrettyTable()
x.add_column("ServiceID",[serviceID])
x.add_column("Service", [services])
x.add_column("Price", [price])
while True:
try:
ID = random.randint(1,90000) #range high to lower probability of non-uniqueness
serviceID.append(ID) #Generates unique ID for each service
prompt1 = input("Please add a service name to the list\n")
services.append(prompt1)
prompt2 = input("Please enter a price for the service\n")
prompt2 == int(prompt2)
price.append(prompt2)
print(x)
except ValueError:
print("Please enter valid type")
continue
当我输入第一个服务和价格时,输出为:
+-----------+---------+--------+
| ServiceID | Service | Price |
+-----------+---------+--------+
| [9880] | ['box'] | ['90'] |
+-----------+---------+--------+
当我输入第二个服务和价格时,输出如下:
+---------------+-----------------+--------------+
| ServiceID | Service | Price |
+---------------+-----------------+--------------+
| [9880, 47612] | ['box', 'trim'] | ['90', '80'] |
+---------------+-----------------+--------------+
我希望输出如下:
+---------------+-----------------+--------------+
| ServiceID | Service | Price |
+---------------+-----------------+--------------+
| 9880 | box | 90 |
| 47612 | trim | 80 |
+---------------+-----------------+--------------+
有人知道如何做到这一点吗? 任何帮助都将不胜感激。
我总是在
while
循环中创建类prettytable
.PrettyTable的新实例,从而实现了这一点。这是我使用方法
field_names()
和add_row()
编写的代码版本。相关问题 更多 >
编程相关推荐