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.