Search code examples
pythonappendraw-inputprettytable

How to raw input to dictionary


I'm new to python, is there a way to modify this code to the point I can keyboard input movies and their rating and it appends to the row in the table?

from prettytable import PrettyTable

x = PrettyTable(["Moive Title", "Ratings"])
x.padding_width = 1 # One space between column edges and contents (default)
x.add_row(["Inception", "5 Stars"])
x.add_row(["Training Day","5 Stars"])
x.add_row(["Boyhood", "3 Stars"])
x.add_row(["Interstellar", "4.5 Stars"])

print x

Solution

  • I just made some variables to contain the input data as a tuple. I didn't load pretty table on my pc but I assume you should be able to enter a variable containing your data for each row.

    I hope this helps and doesn't piss anyone off that I posted this as an answer as I felt it was too long for the comments.

    import PrettyTable
    
    firstRow = input ('What would you like to put in your first row sir?')
    secondRow = input ('What would you like to put in your second row sir?')
    thirdRow = input ('What would you like to put in your third row sir?')
    fourthRow = input ('What would you like to put in your fourth row sir?')
    
    x = PrettyTable(["Moive Title", "Ratings"])
    x.padding_width = 1 # One space between column edges and contents (default)
    x.add_row([firstRow])
    x.add_row([secondRow])
    x.add_row([thirdRow])
    x.add_row([fourthRow])
    print (x)