Search code examples
python-3.xpseudocode

pseudocode understanding Mauch lesson


display user message
WHILE TRUE
get score
if score is from 0 to 100
add score to score total
add 1 to number of scores
elseif score i 999
end loop
else print error message
calculate average score
display results

------------------------------------ I wrote it like this----------

#welcome message
print("thank us for testing")
print ("enter 999 to top running")
print ("*"* 20)

while True:
    score = int(input("Enter grad score: "))
    scores = 0 

    if score >=0 and score <=100:
        score += scoreTotal
        scores += 1

    elif score == 999:
        break
    else:
        print("error message")
averageScore = scoreTotal / score.

i'm tryin to learn with this Mauch book, though it's a bit confusing once i got to pseudocode. Can someone explain what i'm doing wrong?


Solution

  • You just have some simple syntactic issues in the order you are adding things,etc. Keeping scores in the loop resets it to 0 every time you loop, which I assume is not what you want. Also, make sure you check the division by zero at the end of the loop. All can be seen below:

    #welcome message
    print("thank us for testing")
    print ("enter 999 to top running")
    print ("*"* 20)
    
    scoreTotal = 0
    scores = 0 #moved out of the loop to maintain value
    
    while True:
        score = int(input("Enter grad score: ")) 
    
        if score >=0 and score <=100:
            scoreTotal += score #equivalent to scoreTotal = scoreTotal + score
            scores += 1
    
        elif score == 999:
            break
        else:
            print("error message")
    #check for division by zero error
    if scores > 0:
        averageScore = scoreTotal / scores
    else:
        averageScore = 0