python - How to add a way to receive a score and return a string based on the score to a function? -


thanks answers. code works fine this.

def rate_score(selection):     if selection < 1000:         return "nothing proud of!"      elif selection >= 1000 , selection < 10000:         return "not bad."      elif selection >= 10000:         return "nice!"  def main():     print "let's rate score."     while true:         selection = int(raw_input('please enter score: '))         break     print 'you entered [ %s ] score' % selection     score = rate_score(selection)     print score  main() 

however need set parameter of rate_score(selection) 0 default , add call rate_score(selection) in pass no value function. have revised code this:

def rate_score(selection):     if selection < 1000:         return "nothing proud of!"      elif selection >= 1000 , selection < 10000:         return "not bad."      elif selection >= 10000:         return "nice!"      else:         selection = 0  selection = int(raw_input("enter score. "))  score = rate_score(selection) print score 

did @ least set default parameter 0? if not how should go changing default parameter of rate_score()? not know how go allowing no value passed rate_score considering error if not enter because of raw_input.

as lasse v. karlsen commented on question, you'll first need replace print return.

you want condition if score is proud of, right? here's that, while receiving score input user:

def rate_score(selection):     if selection < 1000:         return "nothing proud of!"     else:         return "now that's proud of!"  def main():     print "# rate_score program #"     while true:         try:             selection = int(raw_input('please enter score: '))         except valueerror:             print 'the value entered not appear number !'             continue         print 'you entered [ %s ] score' % selection         response = rate_score(selection) # function rate_score returns response         print response # can print response rate_score() returned  main() 

raw_input built-in function in python, can used input user. using raw_input(), , int(), can make sure input user number. because int() throw specific error if try give not number. the error throws called valueerror.

>>> int('notanumber') valueerror: invalid literal int() base 10: 'notanumber' 

by anticipating error (valueerror), , catching except statement, can inform user when our program has determined input did not evaluate number. note in order catch error except statement, expression throws error must evaluated try statement, hence:

try:     # begin code know throw `valueerror`     selection = int(raw_input('please enter score: ')) except valueerror:     # if `valueerror` occurs?     # tell user entered doesn't appear number     print 'the value entered not appear number !'     continue 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -