I'm relatively new to Python Programming, using Python 3.x, and am working on a Barbershop P.O.S system where the admin will have the privilege to add Services and their corresponding Prices. I'm using the Pretty Table library to achieve printing out a table with serviceID, service and price.
Here's my code:
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
When I enter the first service and Price, the output is:
+-----------+---------+--------+
| ServiceID | Service | Price |
+-----------+---------+--------+
| [9880] | ['box'] | ['90'] |
+-----------+---------+--------+
When I enter the 2nd service and price, the output is this:
+---------------+-----------------+--------------+
| ServiceID | Service | Price |
+---------------+-----------------+--------------+
| [9880, 47612] | ['box', 'trim'] | ['90', '80'] |
+---------------+-----------------+--------------+
I'd like the output to be this:
+---------------+-----------------+--------------+
| ServiceID | Service | Price |
+---------------+-----------------+--------------+
| 9880 | box | 90 |
| 47612 | trim | 80 |
+---------------+-----------------+--------------+
Does anyone know how to achieve this? Any help would be appreciated.
We can do by adding row into
PrettyTable
object byadd_row
method.Demo:
Delete Row:
Use
del_row()
method. We need to pass index of row which we want to remove.Need to handle exception if we provide index value greater then total rows in exception will raise, that need to handle in ur code.
Exception is:
Note:
Use
raw_input()
for Python 2.xUse
input()
for Python 3.xI've accomplished that by always creating a new instance of the class prettytable.PrettyTable inside the while loop.
Here is my version of code using methonds field_names() and add_row().