0

I am using below python code:

n = int(input('enter the number:'))
student_marks = {}
for i in range(n):
    name, *line = input().split()
    scores = list(map(float, line))
    student_marks[name] = scores
query_name = input('enter the name:')
list_1 = list(student_marks[query_name])
no = len(l)
print(no)
s = sum(l)
print(s)
ss = s/no
print(ss)

But, i am getting an error while input the query_name during the run of code.

source: https://www.hackerrank.com/challenges/finding-the-percentage/problem

James Z
  • 12,209
  • 10
  • 24
  • 44
Shakti237
  • 3
  • 3
  • What do you expect for name, *line = input().split() to do? – Leo Arad Apr 13 '20 at 17:50
  • Hello Leo, The above code cuts provided input on whitespaces into a list. https://stackoverflow.com/questions/56658837/how-does-name-line-input-split-work-in-python-3 – Shakti237 Apr 13 '20 at 18:11
  • I'm getting the error from the line no = len(l) there is no l define. – Leo Arad Apr 13 '20 at 18:24
  • Sorry, I forgot to replace l by list_1. Can you check now please ? I still getting error message, I might be missing something – Shakti237 Apr 13 '20 at 18:38
  • I was running the code below inserting 3 in the beginning then a 13 14 15 \n (new line) b 16 17 18 \n (new line) c 20 21 22 and after that in the 'enter the name:' a I was receiving 3 \n 42 \n 14 – Leo Arad Apr 13 '20 at 18:54

3 Answers3

0

you can try to do

n = int(input('enter the number:'))
student_marks = {}
for i in range(n):
    name, *line = input("enter name and scroe (spared by space): ").split()
    scores = list(map(float, line))
    student_marks[name] = scores
query_name = input('enter the name:')
list_1 = list(student_marks[query_name])
no = len(list_1)
print("the numer of scores {}".format(no))
s = sum(list_1)
print("The sum of all scores {}".format(s))
ss = s/no
print("The average score {}".format(ss))
Leo Arad
  • 4,452
  • 2
  • 6
  • 17
0
if __name__ == '__main__':
n = int(input())
student_marks = {}
count = 0
for _ in range(n):
    name, *line = input().split()
    scores = list(map(float, line))
    student_marks[name] = scores
query_name = input()
for i in student_marks[query_name]:
    count += i
average = count / len(student_marks[query_name])
print("%.2f" %average)
tanmaychk
  • 37
  • 5
  • Hi! Thanks for contributing an answer but can you also add an explanation on how your code solves the problem and highlight the differences to the OP's try? – Jeanne Dark Nov 30 '20 at 07:48
0
You can try this solution:
--------------------------

from decimal import Decimal
# Main function (like Java main() method)
if __name__ == '__main__':
    # Taking number of times input will be taken from console and converting it into int type
    n = int(input())
    # creating an empty dictionary
    student_marks = {}
    # Iterate from: 0 to n-1 times
    for _ in range(n):
        # Taking the first argument as name and all other numbers inside line var
        name, *line = input().split()
        # Converting the numbers contained in line variable to a map then, converting into list
        scores = list(map(float, line))
        # Inserting into dictionary as key - value pair
        student_marks[name] = scores
    # Taking the student name from console and store into query_name 
    query_name = input()

# Fetch student marks using student name
query_scores = student_marks[query_name]
# Sum all the marks
total_scores = sum(query_scores)
# Find average of the marks
avg = Decimal(total_scores/3)
# print the average upto two decimal point
print(round(avg, 2))
Md. Shahariar Hossen
  • 1,367
  • 10
  • 11