我必须调试这个程序,而对于我的生活,我无法弄明白。我不明白为什么while循环没有运行,当我进入'完成'我只是得到一个值错误。
# Constant Definitions
MAX_SCORE = 10
MIN_SCORE = 0
def GetValidInput ( prompt ):
"""
This function continues to ask the user for valid input until it
it received. The acceptable inputs are integers between 0 and 10
(inclusive) and the string "done"
Preconditions: Prompt is expected to be a string that is printed
out each time the user is asked for a new input.
Postconditions: The valid input is returned as a string.
"""
strScore = input ( prompt )
while ( not str.isdigit(strScore) and strScore != "done" ) or \
( str.isdigit(strScore) and \
( int(strScore) < MIN_SCORE and int(strScore) > MAX_SCORE ) ):
if strScore.isdigit :
print ( "Please enter a number between %0d and %0d." \
% ( MIN_SCORE, MAX_SCORE), end=' ' )
else:
print ( "Please enter only whole numbers.", end=' ' )
strScore = input ( prompt )
return strScore
# Program Instructions
print ( "Enter the homework scores one at a time. Type 'done' when finished." )
allScores = [ 1 ]
strScore = GetValidInput ( "Enter HW#" + str(len( allScores )) + " score: " )
while ( strScore != "done" ):
allScores.append ( int(strScore) / MAX_SCORE )
strScore = GetValidInput ( "Enter HW#" + str(len( allScores )) + " score: " )
letterGrade = "I"
if ( len( allScores ) >= 1 ):
pctScore = sum ( allScores ) // ( len ( allScores ) * 100 )
elif ( len( allScores ) < 1 ):
pctScore = 0
elif ( pctScore < 60 ):
letterGrade = "F"
elif ( pctScore < 70 ):
letterGrade = "D"
elif ( pctScore < 77 ):
letterGrade = "C"
elif ( pctScore < 80 ):
letterGrade = "C+"
elif ( pctScore < 87 ):
letterGrade = "B"
elif ( pctScore < 90 ):
letterGrade = "B+"
else:
letterGrade = "A"
print ( "Your percentage score is %.2f%% and your letter grade for the course is a %0s." \
% ( pctScore, letterGrade ) )
#end
起初我没有正确调用isdigit。我以为我修好了但是我猜它还是错的,因为它还没有用。我不知道还有什么可以尝试,我真的卡住了
答案 0 :(得分:3)
你可能想在这种条件可能出现的条件下进行思考:
int(strScore) < MIN_SCORE and int(strScore) > MAX_SCORE
答案 1 :(得分:0)
首先,您错误地计算了百分比:
pctScore = sum ( allScores ) // ( len ( allScores ) * 100 )
应该是
pctScore = (sum(allScores) // len(allScores)) * 100
其次,这一行:
elif ( pctScore < 60 ):
应该只是if
,否则只有在len(allScores)
不小于或大于或等于1时才会运行,即从不。
最后,请查看PEP-008并明智地格式化您的代码。
答案 2 :(得分:0)
粗略猜测,主要问题是脚本编写为在python 3下运行,请参阅print语句,并在python 2下运行它。
你应该尝试的第一个修复是找到python 3安装,并使用它。如果您不能这样做,请将'input'调用更改为'raw_input',这将修复值errr问题。