Homework 2 Questions

Two questions about the homework:

1) I submitted the following answer for Homework 2.4 (blastoff):

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

print countdown (10)

The only difference from the solution in the video, is that I used "return" instead of "print" in the final line of the problem. My answers was marked incorrectly. Can you explain why print is correct, but return was an incorrect answer?


2) I was also marked incorrectly for the answer I submitted for Homework 2.6 "Find Last" - while slightly less elegant that the solution in the answer video, this appears to do the same thing. Can you explain why this solution below was marked as incorrect?**

def find_last(search_string, target_string):
    pos = search_string.find(target_string)
    while pos >= 0:
        increment = search_string.find(target_string,pos+1)
        if increment != -1:
            pos = increment
        else:
            break
    return pos
print find_last("aaaa", "a")

Thanks - really enjoying the course. Hope you can take a look at these questions and provide some answers. Even if you are unable to change my grade, I would love to learn.

David

asked 12 Mar '12, 23:42

David-23's gravatar image

David-23
122
accept rate: 0%


3 Answers:

Was your code for 2.4 exactly as written, with irregular indentation? That might be enough to fail the automatic grading program. while should be indented and return should be at the same level of indentation as the while loop, like this:

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

Besides that, they may just be picky about making sure we know the distinction between printing a value (which returns None) and returning a value...even if the output looks the same when you run it.

link

answered 12 Mar '12, 23:54

Frank%20Farach's gravatar image

Frank Farach
6911412

Yes, they definitely require that "Blastoff!" be printed within the procedure, not returned and then printed externally.

(13 Mar '12, 02:07) Kenneth I. L... Kenneth%20I.%20Laws-1's gravatar image

Gotcha - thanks for the comments on my 1st question. Any ideas on the second one?

link

answered 13 Mar '12, 04:13

David-23's gravatar image

David-23
122

def find_last(text,such):
    last = -1
    result = 0
    while True:
        result = text.find(such,last+1)
        if result == -1:
            return last
        last = result

This was my solution and it returned as Correct!

link

answered 13 Mar '12, 04:21

Kurt%20E.%20Spahn-2's gravatar image

Kurt E. Spahn-2
183139

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,292
×74

Asked: 12 Mar '12, 23:42

Seen: 92 times

Last updated: 13 Mar '12, 04:21