[Tutor] Question regarding Python loops

Alan Gauld alan.gauld at btinternet.com
Thu Apr 10 13:22:03 CEST 2014


On 10/04/14 03:04, Conner Crowe wrote:

> *Problem 2.*

> # Here's one possible stand-in:
> def leave(minute):
>       if minute > 0:
>            return minute
>       return 0


> So far for this i have:
> *def* *leave*(minute):
> *if* minute > 0:
> *return* minute
> *elif* minute=0:
> *return* 0

Ignoring the messsed yup formatting, presumably due
to HTML mail...
You have introduced a bug into the example you were given.
The elif line should read

elif minute == 0:  # use double = sign

But as the example shows you don't need the explicit check
the function returns zero even for negative numbers.
Your version returns None for negatives which will likely
cause your later code to break.

> *def**already_left*(i):
> *for* i *in* range(0,t)

Where is t defined?

>          leave(i)

You calculate the result but then ignore it...
You need to return a value from your function.

> *Problem 3.*

Lets do one problem at a time.


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



More information about the Tutor mailing list