Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41424

Re: What are some other way to rewrite this if block?

Newsgroups comp.lang.python
Date 2013-03-18 07:18 -0700
References <mailman.3444.1363614983.2939.python-list@python.org>
Message-ID <3dcc50c9-5909-4b10-9eb4-fd0cc5d98f35@googlegroups.com> (permalink)
Subject Re: What are some other way to rewrite this if block?
From "Yves S. Garret" <yoursurrogategod@gmail.com>

Show all headers | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

What are some other way to rewrite this if block? Santosh Kumar <sntshkmr60@gmail.com> - 2013-03-18 19:26 +0530
  Re: What are some other way to rewrite this if block? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-03-18 16:10 +0200
    Re: What are some other way to rewrite this if block? Duncan Booth <duncan.booth@invalid.invalid> - 2013-03-18 14:43 +0000
      Re: What are some other way to rewrite this if block? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-03-18 16:53 +0200
  Re: What are some other way to rewrite this if block? "Yves S. Garret" <yoursurrogategod@gmail.com> - 2013-03-18 07:18 -0700
  Re: What are some other way to rewrite this if block? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-18 15:10 +0000
    Re: What are some other way to rewrite this if block? Chris Angelico <rosuav@gmail.com> - 2013-03-19 02:24 +1100

csiph-web