HW2.4 'return' instead of 'print' and HW2.6 what does 'while True' mean ?

1- HW 2.4: what is wrong with this code? Does it matter that I use 'return' instead of print in this exercise?

def countdown(n):
    i = n
    while  n > 0:
        print n
        n = n - 1
    return 'Blastoff!'

2- HW 2.6: What does "while True" in the code bellow mean? in other word, what value are we testing if it stays 'True' ?

1   def find_last(s,t):
2       last_pos = -1
3       while True:
4           pos = s.find(t, last_pos +1)
5           if pos == -1:
6               return last_pos
7           last_pos= pos

I appreciate any help...

asked 08 Mar '12, 21:16

tondra's gravatar image

tondra
9017
accept rate: 0%

edited 08 Mar '12, 21:31


4 Answers:

First Question:

Blastoff the Bad :-/

Second Question:

While True:

Means it's an infinite loop because the condition is always True :-)

So.. when does it end? In such cases you have to use return or break statements.

link

answered 08 Mar '12, 21:32

Jose%20Alexis%20Valera%20Ruiz-2's gravatar image

Jose Alexis ...
13415

To expand on @mediomelon 's answer, any expression we test in a while loop's condition will evaluate to some boolean value, True or False. Simply putting the value True there is the same as putting an expression there that will evaluate to True, such as while 1 == 1:

The loop will never end until you break out with a statement like break or return.

link

answered 08 Mar '12, 21:34

Thom%20Blake's gravatar image

Thom Blake
4.4k104681

HW 2.4

Yes, it does matter. It should print all output, including string "Blastoff!" when called only like this:

countdown(5)

I.e. it should not be necessary for caller to use also print like this:

print countdown(5)

(This is actually incorrect usage of the countdown function according to assignment, since its return value was unspecified and this kind of call would print extra string "None" or some other thing returned from the function.)

HW 2.6

The True in while's condition means that it will loop forever (i.e. infinite loop) - unless there will be some break statement executed inside the while's block. You probably understand most of this, but anyway, you will find thorough explanation here.

link

answered 08 Mar '12, 21:36

Marek%20B%C3%A1lint's gravatar image

Marek Bálint
4.7k164285

edited 08 Mar '12, 21:50

HW2.4 sure...although, I checked it and it did "print" (show on the screen) when I ran my code. Let's not whine about it. I got blasted off !!!
HW2.6 ...it's an infinite loop, why is that escaping from my mind all the time...?

Thanks!!!

link

answered 08 Mar '12, 21:46

tondra's gravatar image

tondra
9017

Your answer
Question text:

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×15,349
×75
×69
×43
×16

Asked: 08 Mar '12, 21:16

Seen: 293 times

Last updated: 08 Mar '12, 21:50