Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python program that takes in a Name and 10 Scores and Returns the Name and 6 highest Scores

name = input("Enter name: ")  
scores = input("Enter 10 test scores separated by commas: ")  
data = zip (name, scores)
for (student, score) in zip(names, scores): pass


print(name)  
print(scores)

Input name prompt comes up and I enter name and then name is undefined?

Traceback (most recent call last):  
  File "C:\Users\Scott\Desktop\CIS   129\vandress_m8assignment\vandress_m8.py", line 4, in <module>  
    for (student, score) in zip(names, scores): pass  
NameError: name 'names' is not defined. Did you mean: 'name'?  

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

An efficient way to achieve this would be with the following code:

def Top6TestScoresOutof10():  
    name = input("Enter name: ")  
    data = input("Enter 10 test scores separated by commas: ")  
    scores = [int(score) for score in data.split()]
    heapq.heapify(scores)
    return name, heapq.nlargest(6, scores)
    

name, scores = Top6TestScoresOutof10()  
print(name)  
print(scores)

There’s a bit going on here, so I’ll explain it line by line. Let’s start with the
scores = [int(score) for score in data.split()] line. First you’ll want to understand list comprehension. This is the process of building a list in place using the syntax [x for x in some_iterable]. Next, the data.split() will covert the string supplied by the user by whitespace, so "a b c d e food" becomes ['a', 'b','c', 'd', 'e', 'food']. And int(score) performs type casting on the string, to convert it from a string to an integer. so if we enter 45 24 20 48 100, scores will be equal to a list of integers, [45, 24, 20, 48, 100]. Having these numbers as integers is important, since one of your requirements is that you want to collect the top 6.

After this, we’re using the python heapq library, which "provides an implementation of the heap queue algorithm". Heaps are an extremely powerful data structure for problems just like this one, I would definitely recommend reading up on them here. With the heapq.heapify(scores) line, we’re performing an inline, linear time transformation of our list into a heap. This allows us to quickly pick the nlargest values from our list. Thus, on the next line, we return the name and heapq.nlargest(6, scores).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading