Home > other >  Having trouble with some homework
Having trouble with some 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:

Try this one:

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

exams = examCount
totalScore = 0

while examCount > 0:
    examScore = int(input("Input your exam score."))
    examCount -= 1
    totalScore  = examScore

print(totalScore/exams)

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

CodePudding user response:

a = int(input("How many scores you wanna record "))
res = []

flag = False
b=1
while not flag:
    
    if b == a:
        flag = True
        
    res.append(int(input("enter score ")))
    b =1
print(sum(res)/len(res))

CodePudding user response:

First line says that:

examNum = examCount # NameError, we didn't declare examCount

Fifth line says:

totalScore = sum(examScore) # NameError, we didn't declare examScore

And the syntax Syntax mistakes at line 9-10:

The else keyword can be used only if there is keyword above on the same indentation level:

# Same level of indentation between while, and else:
....while True:
        pass
....else:
        pass

# Not same:
....while True:
        pass
........else:
            pass

And arguments must be passed into print statement via comas

print("Your average score is", totalScore)

CodePudding user response:

examCount = int(input("How many scores would you like to enter? "))
totalScore = 0
i = 0
while i < examCount:
    examScore = int(input("Input your exam score: "))
    totalScore  = examScore
    i  = 1
averageScore = totalScore / examCount
print("Your average score is ", averageScore)

I hope this will fix your issue!

  • Related