伪代码理解Mauch课

时间:2018-07-10 18:02:13

标签: python-3.x pseudocode

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

------------------------------------我这样写------ ----

#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.

我正在尝试学习这本Mauch的书,尽管一旦我获得了伪代码,这有点令人困惑。有人可以解释我在做什么错吗?

1 个答案:

答案 0 :(得分:0)

按照添加顺序等,您仅遇到一些简单的语法问题。每次循环时,将分数保留在循环中会将其重置为0,我认为这不是您想要的。另外,请确保在循环结束时检查除以零。都可以在下面看到:

#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