0

I have created a program that calculates the average of a student, and I want input to be created based on the user input, for example: if the user types 5, it creates 5 input.

print('Average of students')

def student_average():
    while True:
        try:
            # I want to create inputs depending on the user's input in the following question:
            number_of_qualifications = int(input('What is the number of qualifications?: '))
            qualification_1 = float(input('What is the first qualification?: '))
            qualification_2 = float(input('What is the second qualification?: '))
            qualification_3 = float(input('What is the third qualification?: '))
            qualification_4 = float(input('What is the fourth qualification?: '))
            break
        except:
            print('This is not an option')
    sum = (qualification_1 + qualification_2 + qualification_3 + qualification_4)
    average = (sum / 4)
    print(average)

student_average()
1
  • You can create a loop for the number_of_qualifications entered and store qualifications into an array. Commented Mar 20, 2022 at 2:52

3 Answers 3

3

You need to use a loop. I've removed the try/except, you can add that back in if you want.

def student_average():
    number_of_qualifications = int(input('What is the number of qualifications?: '))
    sumx = 0
    for _ in range(number_of_qualifications):
        sumx += float(input('What is the next qualification?: '))
    return sumx / number_of_qualifications

print(student_average())
Sign up to request clarification or add additional context in comments.

5 Comments

may I know how do you implement try-except in a for-loop? If an exception occurs, the loop still increases by 1 so 1 data point is lost, so I cannot resolve this
You can put the try/except inside the for loop.
would you show me how please? Try with 5 inputs: 10, 20, 30, aa (exception), 40, 50. The average should be 30, but I'm unable to input the last one (50)
while True/try/input/break/except/print.
that solution is while-loop, NOT for-loop. I think it's only possible in while-loop
0

I have modified your codes, so the try-except feature is preserved

def student_average():
    total, c = 0,0
    number_of_qualifications = int(input('What is the number of qualifications?: '))
    while True:
        try:
            # I want to create inputs depending on the user's input in the following question:
            total += float(input(f'{c+1} What is the qualification?: '))
            c += 1
            if c == number_of_qualifications:
                break
        except:
            print('This is not an option')
    average = (total / number_of_qualifications)
    print(average)
    
print('Average of students')
student_average()

Output:

Average of students
What is the number of qualifications?:  3
1 What is the qualification?:  4
2 What is the qualification?:  5
3 What is the qualification?:  a
This is not an option
3 What is the qualification?:  6
5.0

Comments

0

Please do not use "sum" as a variable, because it is a Python reserved word. If it has been used before, you have to restart the Kernel.

def student_average():
    cnt = 1
    total_list = []
    number_of_qualifications = int(input('What is the number of qualifications?: '))
    while len(total_list) < number_of_qualifications:
        try:
            # I want to create inputs depending on the user's input in the following question:
            total_list.append(float(input(f'{cnt}. What is the qualification?: ')))
            cnt += 1
        except:
            print('This is not an option')
    return (sum(total_list) / number_of_qualifications)
    
print('Average of students =', student_average())

Output

What is the number of qualifications?:  5
1. What is the qualification?:  10
2. What is the qualification?:  20
3. What is the qualification?:  30
4. What is the qualification?:  aa
This is not an option
4. What is the qualification?:  40
5. What is the qualification?:  50
Average of students = 30.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.