0

I found this on HackerRank.

I had to type the no. of students (say n). Then the following n lines each had Student name, and marks of three subjects to be input by the user. The next line consisted of the name of any of the students. Based on that input, I had to calculate the average of that student's marks.I had to provide an input like this(according to the testcases):

4
nameA 50 50 50
nameB 69 78 45
nameC 56 47 45 
nameD 57 49 30
nameC

As I had to give mutiple type inputs on the same line, while I tried to append the list, it gave me an 'invalid literal' error. So I used input().split(" "). This is my code:

if __name__ == '__main__':

n = int(input())
list1 = []
for i in range(0,n):
    a = input().split(" ")
    b = input().split(" ")
    c = input().split(" ")
    d = input().split(" ")

    b = float(b)
    c = float(c)
    d = float(d)

    list1.append(a)
    list1.append(b)
    list1.append(c)
    list1.append(d)


name = input().split()
avg = 0.00
inn = list1.index(name)

avg = (list1[inn+1]+list1[inn+2]+list2[inn+3])/3

print(avg)

But this was the error displayed:

Traceback (most recent call last):
File "solution.py", line 11, in <module>
b = float(b)
TypeError: float() argument must be a string or a number, not 'list'

ON the other hand if I simply give the input using input(), there is the 'invalid literal with base 10' error again. What is going wrong with typecasting? And how can I give different types of inputs in a single line(i.e using spaces). I tried to do this question using nested lists, too. But if I have to append the nested list, what must be the syntax?

Shady45
  • 45
  • 1
  • 10
  • Possible duplicate of [In Python, how do I convert all of the items in a list to floats?](https://stackoverflow.com/questions/1614236/in-python-how-do-i-convert-all-of-the-items-in-a-list-to-floats) – Mike Scotty Jun 06 '18 at 12:52

2 Answers2

1

b = input().split(" ") b variable is of type list, split() method make list of values, you can do something like this

#for example variable `a` is [1,2,3,4]
# make new list with `float`
a_float_lst = [float(x) for x in a]

# and now concatenate with our `list_1` list
list_1 += a_float_lst

Update

if __name__ == '__main__':
    lst = []
    for i in range(int(input())):
        student, b, c, d = input().split()
        lst.append([student, float(b), float(c), float(d)])

    name = input()
    inn = None
    for x in lst:
        if name in x:
            inn = x

    avg = (inn[1] + inn[2] + inn[3]) / 3
    print('{:.2f}'.format(avg))

Input

2
nameA 50 50 50
nameB 69 78 45
nameA

Output

50.0
Druta Ruslan
  • 7,171
  • 2
  • 28
  • 38
  • Yes, but I think we can do this for a single student only. If we have more than one student, we'll be creating a list of lists, right? Anyways, I tried your way and gave the input in a slightly modified manner: `n = int(input()) list1 = [] for i in range(0,n): name = str(input()) list1.append(float(input())) list1.append(float(input())) list1.append(float(input())) list1 = name + list1` But it threw an arror: could not convert string to float 'Arjun 45 66 78' – Shady45 Jun 06 '18 at 13:11
  • @Shady45 give me some minutes – Druta Ruslan Jun 06 '18 at 13:20
  • Also, the name is a single word. I want that input to end when I press space. How can I do that? If I do not use split, it takes the float marks as continuation of string input, hence unable to distinguish between name and marks. – Shady45 Jun 06 '18 at 13:22
  • @Shady45 something like this ? – Druta Ruslan Jun 06 '18 at 13:36
  • Thanks a lot! There's just another small issue: The answer is expected to be 50.00. How can I get one more decimal digit? I tried many methods, like dividing the sum of marks by 3.00, or defining avg = 0.00, but it won't work. – Shady45 Jun 06 '18 at 14:02
1

A call to input() will fetch you one line like nameA 50 50 50. Doing a split on that would give you a list like ['nameA', '50', '50','50'].

What you want to do is extract name and numbers from a single line. Something like this:

a, b, c, d = input().split( " " )
Mohammad Azim
  • 2,604
  • 20
  • 21