答えてくれてありがとう。このコードはこれでうまく機能します。
def rate_score(selection):
if selection < 1000:
return "Nothing to be proud of!"
elif selection >= 1000 and selection < 10000:
return "Not bad."
elif selection >= 10000:
return "Nice!"
def main():
print "Let's rate your score."
while True:
selection = int(raw_input('Please enter your score: '))
break
print 'You entered [ %s ] as your score' % selection
score = rate_score(selection)
print score
main()
ただし、rate_score(selection) のパラメーターをデフォルトとして 0 に設定し、関数に値を渡さない rate_score(selection) への呼び出しを追加する必要もあります。コードを次のように修正しました。
def rate_score(selection):
if selection < 1000:
return "Nothing to be proud of!"
elif selection >= 1000 and selection < 10000:
return "Not bad."
elif selection >= 10000:
return "Nice!"
else:
selection = 0
selection = int(raw_input("Enter your score. "))
score = rate_score(selection)
print score
少なくとも、デフォルトのパラメーターが 0 になるように設定しましたか? そうでない場合は、rate_score() のデフォルト パラメータに変更するにはどうすればよいですか? また、raw_inputのために何も入力しないとエラーが発生することを考慮して、rate_scoreに値を渡さないようにする方法もわかりません。