感谢您的回答。此代码适用于此。
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 传递任何值。