Homework 4.7

Why Cant we just have a break statement?

My code :

def add_to_index(index, keyword, url):  
    for entry in index:  
         if entry[0] == keyword:  
            entry[1].append(url)  
            return  
        break  
    # not found, add new keyword to index  
    index.append([keyword, [url]])

asked 20 Mar '12, 23:56

Kanika%20Kharbanda-1's gravatar image

Kanika Kharb...
4612717
accept rate: 37%

edited 21 Mar '12, 05:08

JohanG-Sweden's gravatar image

JohanG-Sweden
9.4k1242100

I formatted the code.

(21 Mar '12, 05:08) JohanG-Sweden JohanG-Sweden's gravatar image

3 Answers:

Why would you be breaking out of the loop before going through all of the different entry[0] values?

link

answered 21 Mar '12, 00:02

Stuart%20M.%20Schram%20IV-1's gravatar image

Stuart M. Sc...
2.9k2619

You may want to indent the code to see where you have return & break. Just highlight the code after you paste in the text entry window and click the format button on the top that has 101010. This information is thanks to a gentleman who pointed out this to me couple weeks back.

link

answered 21 Mar '12, 00:04

Arvind%20Deogirikar-2's gravatar image

Arvind Deogi...
6571727

You used a return & then a break?

I'm not sure about the indenting, but I think the break statement stops the function from getting to the last statement, which means nothing is appended to your index if the keyword isn't already present in the index.

Also, you needed to add a counter to every url. You don't do that here.

link

answered 21 Mar '12, 00:04

elssar's gravatar image

elssar
19.2k2563155

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,224
×161
×41

Asked: 20 Mar '12, 23:56

Seen: 151 times

Last updated: 21 Mar '12, 05:08