Python Forum
making variables in my columns and rows in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
making variables in my columns and rows in python
#1
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?

#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.
Reply


Messages In This Thread
making variables in my columns and rows in python - by kronhamilton - Oct-31-2021, 03:28 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to store columns of a .csv in variables (when the .csv has no headers) ? hobbyist 6 1,375 Aug-18-2023, 02:06 PM
Last Post: deanhystad
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,845 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 1,986 Dec-12-2022, 08:22 PM
Last Post: jh67
  Check DataFrames with different sorting in columns and rows Foxyskippy 0 815 Nov-19-2022, 07:49 AM
Last Post: Foxyskippy
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,655 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  rows from sql query need to write to a file as columns sjcsvatt 6 2,499 Oct-09-2021, 12:45 AM
Last Post: snippsat
  Merging spreadsheets with the same columns and extracting rows with matching entries johnbernard 3 10,920 Aug-19-2021, 03:08 PM
Last Post: johnbernard
  Summing up rows and columns plumberpy 3 2,324 Aug-18-2021, 05:46 AM
Last Post: naughtyCat
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 2,157 May-04-2021, 10:51 PM
Last Post: rhat398
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 7,330 Mar-02-2021, 01:54 AM
Last Post: Jeremy7

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020