Oct-31-2021, 03:28 AM
I've been searching the internet for the answer to my problem, but to no avail. I'm trying to put data into rows and columns, but I have variables and I thought that I had found the answer to my problem, but it wasn't a solution to the variable part of it. Is there a way that I could modify this method a little bit to include variables or is there another way to do this?
thanks ahead of time.
#inputs name = input("Who are you? ") supermarket = input("Where are you going? ") date = input("What day is it? ") item = input("What are you buying? ") price = float(input("How much is the item you are buying? ")) quantity = float(input("How many are you buying? ")) vehicle = input("How do you plan to get there? ") commute_cost = float(input("How much does that cost one way? ")) #process total = price * quantity total_commute_cost = commute_cost * 2 trip_total = total + total_commute_cost #data for column data = '''\ Item Price Quantity Total item price quantity total''' #making the table into columns and rows rows = [ line.strip().split(' ') for line in data.split('\n') ] cols = zip(*rows) col_widths = [ max(len(value) for value in col) for col in cols ] format = ' '.join(['%%%ds' % width for width in col_widths ]) #output print(" ") print("Shopper name:", name) print(" ") print(supermarket) print(date) print(" ") for row in rows: print (format % tuple(row))with an output of
Output:Item Price Quantity Total
item price quantity total
but I want an output, if the variables have been answered by the user,Output:Item Price Quantity Total
Milk 4 2 8
im having a real hard time trying to find anything about it.thanks ahead of time.