简体   繁体   中英

Averaging inputs from user

The fourth assignment involves writing a Python program to compute the average quiz grade for a group of five students. Your program should include a list of five names. Using a for loop, it should successively prompt the user for the quiz grade for each of the five students. Each prompt should include the name of the student whose quiz grade is to be input. It should compute and display the average of those five grades and the highest grade. You should decide on the names of the five students.

So this is my assignment and below is what I have so far.

# This program will compute the average quiz grade for 5 students
print ("Hello, this program is designed to compute the average quiz grades for the following students: Clark, Nicole, Kiran, Alex, and Erik")

students = ["Clark", "Nicole", "Kiran", "Alex", "Erik"]
for student in students:
    print (student, ", What was your quiz grade?")
    grades = eval(input("My grade is: "))

The program asks each student what their grade is. But, I need to take the numbers and add them somehow so I can divide them afterwards for the average. Can anyone advise me on the right path to fixing this?

Yeah. First of all, I've fixed and formatted your code for you.

# This program will compute the average quiz grade for 5 students
print("Hello, this program is designed to compute the average quiz grades for the following students: Clark, Nicole, Kiran, Alex, and Erik")

students = ["Clark", "Nicole", "Kiran", "Alex", "Erik"]

for student in students:
    print(student, ", What was your quiz grade?")
    grades = input("My grade is: ")

So far, you can ask each of the students for a grade. Once they tell you, you should store that inside memory. A simple way to do this would be to use a list (I assume you have not learned dictionaries yet).

You can create a new list before asking for the grades, then add to the end of the list with every new grade a user inputs.

# This program will compute the average quiz grade for 5 students
print("Hello, this program is designed to compute the average quiz grades for the following students: Clark, Nicole, Kiran, Alex, and Erik")

students = ["Clark", "Nicole", "Kiran", "Alex", "Erik"]

grades = []  # create an empty list to store the grades in

for student in students:
    print(student, ", What was your quiz grade?")
    grade = input("My grade is: ")
    grades.append(int(grade))  # convert the grade to an integer number, then add it to the list

print("Here are the grades you entered: ", grades)

Now, we will need to compute for the average of the list. Recalling elementary math, the average of a set of numbers is the sum of the numbers, divided by the number of numbers there are.

In Python, you can use the built-in function sum() to calculate the sum of the numbers. You can use the built-in function len() to get the number of elements in the list.

Now, you will just have to divide the sum by the length.

average = sum(grades) / len(grades)
print("The average is:", average)

Now, in order to get the highest grade, you can use the built-in function max() . This will find and return the biggest number in the array.

You can use it like this.

highest = max(grades)
print("The highest grade is:", highest)

In case you want the combined code:

# This program will compute the average quiz grade for 5 students
print("Hello, this program is designed to compute the average quiz grades for the following students: Clark, Nicole, Kiran, Alex, and Erik")

students = ["Clark", "Nicole", "Kiran", "Alex", "Erik"]

grades = []  # create an empty list to store the grades in

for student in students:
    print(student, ", What was your quiz grade?")
    grade = input("My grade is: ")
    grades.append(int(grade))  # convert the grade to an integer number, then add it to the list

print("Here are the grades you entered: ", grades)

average = sum(grades) / len(grades)
print("The average is:", average)

highest = max(grades)
print("The highest grade is:", highest)

有没有办法在不存储它们的情况下运行此代码并计算结果?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM