What are some other way to rewrite this if block?

Yves S. Garret yoursurrogategod at gmail.com
Mon Mar 18 15:18:57 CET 2013


On Monday, March 18, 2013 9:56:16 AM UTC-4, Santosh Kumar wrote:
> This simple script is about a public transport, here is the code:
> 
> 
> 
> def report_status(should_be_on, came_on):
> 
>     if should_be_on < 0.0 or should_be_on > 24.0 or came_on < 0.0 or
> 
> came_on > 24.0:
> 
>         return 'time not in range'
> 
>     elif should_be_on == came_on:
> 
>         return 'on time'
> 
>     elif should_be_on > came_on:
> 
>         return 'early'
> 
>     elif should_be_on < came_on:
> 
>         return 'delayed'
> 
>     else:
> 
>         return 'something might be wrong'
> 
> 
> 
> print(report_status(123, 12.0))
> 
> 
> 
> I am looking forward of make the line starting with `if` short.
> 
> 
> 
> Any tips are welcome.

If you have a lot of conditions to check, you can't really get around it.



More information about the Python-list mailing list