0

I have some code for a simple rev counter on the Microbit. When returning the variable rev_per_second, sometimes it is a NoneType (for reasons unknown). I try to trap this within the function, but it doesn't seem to work. Where am I going wrong please.

# part of the function revs()  

     if end_time < running_time():   
         try:
             rev_per_second = rev_per_second
         except TypeError:
             rev_per_second = 1    
         return rev_per_second  # make this value available to the main program

# main part of the program            
while True:
    rev_per_min = revs() # sometimes this is "None" Why??
    display.scroll(str(rev_per_min))
Andrii Rusanov
  • 4,405
  • 2
  • 34
  • 54
Bobh46320
  • 9
  • 2
  • There is not enough context here to be sure, but most likely, something is initialising or re-setting `revs_per_second` to "None". – Sean Houlihane Nov 14 '16 at 16:22
  • rev_per_second is set to 1 at the top of the function before the while statement, so what could set it to None ? – Bobh46320 Nov 14 '16 at 18:46
  • Reduce your code to a minimal reproducible example, or link to the full code on github. We can't guess the answer. – Sean Houlihane Nov 14 '16 at 19:26
  • sorry, I had to reject your edit to my answer - you should either comment on my answer, or up-vote it (assuming, as I interpret, it helped). You can also either answer the question yourself, or edit the question to make it clearer. Sorry if it seems a bit convoluted, the idea is to generate a searchable resource of useful Q&A. Also, for reference, you might need to @ mention other users if you want them to see your comment. – Sean Houlihane Dec 02 '16 at 17:07

1 Answers1

0

The explanation is almost certainly that there is a path through the function that does not end in a return statement, so Python by default returns None.

Check that all paths from your revs() function end at a return statement, otherwise Python will return None if there is no return statement.

Sean Houlihane
  • 1,698
  • 16
  • 22