I have an assignment
(Find the highest score) Write a program that prompts the user to enter the number of students and each student’s score, and displays the highest score. Assume that the input is stored in a file named score.txt, and the program obtains the input from the file.
However we haven't learned how to store data in a .txt file yet so the teacher told us to just treat it as inputs.
What I have so far:
grade = 0
students = eval(input("enter number of students: "))
for i in range(1, students 1):
grade = eval(input("enter student score: "))
I understand how to input number of students and store it in to a variable (students).
And I know how to write a loop to repeatedly ask for the input (student score) equal to the number of students
but I don't know how to store each students score separately other than doing it once under the for loop (grade).
CodePudding user response:
students = int(input("enter number of students: "))
highest_grade = 0
for i in range(1, students 1):
grade = int(input("enter student score: "))
if grade > highest_grade:
highest_grade = grade
print("Highest score is {}".format(highest_grade))
I just run and tested. Its working fine
CodePudding user response:
Well you can also try using dictionary. Store student names as well as their scores. Then check for the highest score. You code:
students=int(input ("Enter no of students: "))
scores={}
hg=0
for i in range(students):
name=input("Enter name of student= ")
score=int(input ("Enter score: "))
scores[name]=score
if score>hg:
hg=score
print("Highest score is ",hg)
Explaination: we'll take input for both name as well as score from user and store it in dictionary. By default, we'll save hg(highest score) as 0. Then while taking user input for score, we'll check whether it greater than hg, if yes, update hg to new score and keep checking it until the loop terminates and finally print it
You do this stuff without dictionary and list, I just suggested dictionary because it can save data in more systematic way showing both name as well as score