Home > OS >  Having trouble with some Python homework
Having trouble with some Python homework

Time:09-19

Question: Write a python program using a "While" loop that will ask for the user to input exam scores. You should start by asking the user how many scores they would like to enter. Then use a loop to request each score and add it to a total. Finally, calculate and display the average for entered scores. The current code I have got is listed below though whenever I try to run it to see if it works, is comes up with Syntax Error: Invalid Syntax on the else. I tried without else inside and outside the while loop and it came up with SyntaxErorr: Invalid Syntax. Perhaps you forgot a comma?

examNum = examCount
examCount = int(input("How many scores would you like to enter?"))

totalExam = (examCount   1)
totalScore = sum(examScore)

while examNum  < examCount:
    examScore = int(input("Input your exam score."))
    else:
        print("Your average score is" totalScore)

Note: This is my first time doing Python in 5 - 10 years so I am back to square one with figuring everything else. I'm sure there is a obvious and easy work around but I'd appreciate any help.

CodePudding user response:

exam_count = int(input("How many scores would you like to enter? "))
exam_score = 0

for i in range(0, exam_count):
    exam_score  = int(input("Input your exam score: "))

print(f'Average score is {exam_score/exam_count}')

And just as a reminder for you, else is only used alongside if statements. Using it by itself doesn't do anything.

CodePudding user response:

# taking user input for total no of score of exam one want to count
socre_count = int(input("how many scores you would like to enter:" ))
# saving sum of scores in total_score
total_score = 0
# using index for making a loop
index = 0
# using while loop to iterate
while index < socre_count:
    # getting user input for each exam score
    exam_score = int(input("your exam score: "))
    total_score  = exam_score # adding each score value to total
    index =1 # increasing index
# calculating avg of total score
avg_score = total_score/socre_count
print(f"avg score is {avg_score}") # printing result
  • Related