[Tutor] Help with if-elif-else structure

Alan Gauld alan.gauld at btinternet.com
Thu Aug 25 10:38:11 CEST 2011


On 25/08/11 08:51, Robert Sjoblom wrote:


> If I roll two sixes (on the initial roll) and below the target number
> (in total), it's a failure.
> If I roll two sixes (on the initial roll) and above the target number
> (in total), it's a critical failure.
> If I roll two ones (on the initial roll) and above the target number
> (in total), it's a success.
> If I roll two ones (on the initial roll) and below the target number
> (in total), it's a critical success.
> If I roll below the target number (in total), it's a success.
> If I roll above the target number (in total), it's a failure.
>
> I've written a horrible-looking piece of code to solve this, but I'm
> sure there are better ways to structure it:
>
> #assuming target number 15
> roll = (result, initial_mins, initial_max)

I'd forget the tuple and just use the names,
it is more readable that way...

> if roll[0]>  15:
>      if roll[1]>= 2:
>          print("Success")
>      elif roll[2]>= 2:
>          print("Critical failure!")

if result > 15:
     if initial_mins >= 2:...
     elif initial_max >=2:...

But otherwise it seems to reflect the rules as you've written them...


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list