Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Link to the challenge(Already solved, however, I am trying to do additional stuff)
https://www.hackerrank.com/challenges/py-collections-namedtuple/problem?isFullScreen=true[^]

The first line contains an integer - the total number of students.
The second line contains the names of the columns in any order.
The next lines contains the MARKS, CLASS, NAME, ID, under their respective column names.



The Inputs
5
MARKS      CLASS      NAME       ID        
92         2          Calum      1         
82         5          Scott      2         
94         2          Jason      3         
55         8          Glenn      4         
82         2          Fergus     5


My code
n = int(input())
Tuple1 = namedtuple("Cols", input())
s = 0
Names = []

for i in range(n):
    s += int(Tuple1(*input().split()).MARKS)
    Names += list(Tuple1(*input()).NAME)
    print(s)
    print(Names)
print(s/n) 


It works perfectly for MARKS, however I have tried everything and can't seem to get a LIST of the NAME(s). At best I can get a list with "Property bla bla" x 5

Ex.
If I change the Names part in the for loop to:
Names = str(Tuple1.NAME)

I get a nice list of:
<property object at 0x0000000000eb4320>


I don't know why NamedTuples are killing me.

What I have tried:

Names = str(Tuple1.NAME)
and around 12 other variations with list, tuple, set etc


The best I can get is this:
n = int(input())
Tuple1 = namedtuple("Cols", input())
s = 0
Names = []

for i in range(n):
    s += int(Tuple1(*input().split()).MARKS)
    Names.append(str(Tuple1(*input().split()).NAME))
    print(s)
    print(Names)
print(s/n)


Which will give me two grades and two names separated by a new line.
97

['Steven']'
188

['Steven', 'Stewart']


Before I get an EOF error and the code breaks.
Posted
Updated 24-Aug-22 20:39pm
v6

1 solution

Try
Python
n = int(input())
Tuple1 = namedtuple("Cols", input())
s = 0
Names = []

for i in range(n):
    sp = Tuple1(*input().split())
    s += int(sp.MARKS)
    Names.append(sp.NAME)
    print(s)
    print(Names)
print(s/n)
 
Share this answer
 
Comments
Chris Aug2022 25-Aug-22 2:54am    
Thank you.
CPallini 25-Aug-22 2:59am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900