Home > Mobile >  How to print the elements of a list in a tabular form
How to print the elements of a list in a tabular form

Time:04-12

I want the values of a list to be shown in a tabular form like this:

Ques no. marks
1 4.0
2 -1.0
l = []
print("no of question you want to process")
p = int(input())
y = float(input("enter the value for positive marks(y) :"))
n = float(input("enter the value for negative marks(n) :"))
x = 0
print("For yes type y, for no type n and for no reponse/unattempted type x ")
for i in range(0, p):
    print("enter your answer of ques", (i 1), ":")
    m = input()
    if m == "y":
        l.append(y)
    if m == "n":
        l.append(n)
    if m == "x":
        l.append(x)
print("your total marks are", sum(l))

CodePudding user response:

If I understand you correctly you want to print the final data in tabular form. You can use str.format for that, for example:

def print_tab(data):
    print("{:<30} | {:<30}".format("Ques no.", "marks"))
    print("-" * 63)
    for i, a in enumerate(data, 1):
        print("{:<30} | {:< 30.1f}".format(i, a))
    print("-" * 63)


l = []
p = int(input("no of question you want to process :"))
y = float(input("enter the value for positive marks(y) :"))
n = float(input("enter the value for negative marks(n) :"))
x = 0
print("For yes type y, for no type n and for no reponse/unattempted type x ")

for i in range(0, p):
    print("enter your answer of ques", (i   1), ":")
    m = input()
    if m == "y":
        l.append(y)
    if m == "n":
        l.append(n)
    if m == "x":
        l.append(x)

print("your total marks are", sum(l))
print_tab(l)

Prints:

no of question you want to process :2
enter the value for positive marks(y) :4
enter the value for negative marks(n) :-1
For yes type y, for no type n and for no reponse/unattempted type x 
enter your answer of ques 1 :
y
enter your answer of ques 2 :
n
your total marks are 3.0
Ques no.                       | marks                         
---------------------------------------------------------------
1                              |  4.0                          
2                              | -1.0                          
---------------------------------------------------------------

CodePudding user response:

you can use pandas for that. I added an else statement in case the input is invalid.

import numpy as np
import pandas as pd
l=[]
print("no of question you want to process")
p=int(input())
y=float(input("enter the value for positive marks(y) :"))
n=float(input("enter the value for negative marks(n) :"))
x=0
print("For yes type y, for no type n and for no reponse/unattempted type x ")
for i in range(0,p):
    print("enter your answer of ques",(i 1),":")
    m=input()
    if m == "y":
        l.append(y)
    elif m == "n":
        l.append(n)
    elif m == "x":
        l.append(x)
    else:
        l.append(np.nan)
print("your total marks are" , sum(l) )

df1 = pd.DataFrame(l, columns=['marks'])
df1.index.name =  'Ques no.'
df1

output:

            marks
Ques no.    
0           4.0
1          -1.0
  • Related