我对Python编程相对较新,使用的是Python 3.x版本,正在开发一款理发店收银系统,管理员将有特权添加服务及其对应价格。我正在使用Pretty Table库来打印一个包含服务ID、服务和价格的表格。
以下是我的代码:
以下是我的代码:
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 |
+---------------+-----------------+--------------+
有谁知道如何实现这个? 任何帮助将不胜感激。
"\n".join(your_sublist)
将列表中的字符串连接起来。 - Padraic Cunningham