Can anyone explain for me why this code didn't work?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 2017 Views - Last Post: 06 June 2013 - 08:37 AM Rate Topic: -----

#1 Connectification   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-June 13

Can anyone explain for me why this code didn't work?

Posted 02 June 2013 - 08:30 AM

# Define a procedure, measure_udacity,
# that takes as its input a list of strings,
# and returns a number that is a count
# of the number of elements in the input
# list that start with the uppercase
# letter 'U'.

Can anyone explain for me why this code didn't work?

def measure_udacity(list):
    new_list = []
    for i in list:
        if list[i] == "U":
            new_list = new_list.append(list[i])
    return new_list


Thanks :)/>
Connectification

This post has been edited by JackOfAllTrades: 04 June 2013 - 04:59 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Can anyone explain for me why this code didn't work?

#2 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2644
  • View blog
  • Posts: 4,224
  • Joined: 21-June 11

Re: Can anyone explain for me why this code didn't work?

Posted 02 June 2013 - 08:47 AM

Your code doesn't work because append returns Nothing. So when you write new_list = new_list.append(list[i]), that's the same as if you wrote

new_list.append(list[i])
new_list = Nothing



If that code executes more than once, this will cause an error because Nothing does not have an append method. And even if it executes only once, it won't do what you want because you don't want new_list to be Nothing.

So to solve this issue, simply don't reassign new_list. Just write new_list.append(list[i]) without an assignment.
Was This Post Helpful? 1
  • +
  • -

#3 Connectification   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-June 13

Re: Can anyone explain for me why this code didn't work?

Posted 02 June 2013 - 08:59 AM

Hey sep2k,

I got the new error message now:
if (list[i] == "U"):
TypeError: list indices must be integers, not str

What do you think about this one?
Was This Post Helpful? 0
  • +
  • -

#4 Mekire   User is offline

  • D.I.C Head

Reputation: 118
  • View blog
  • Posts: 216
  • Joined: 11-January 13

Re: Can anyone explain for me why this code didn't work?

Posted 02 June 2013 - 09:09 AM

Firstly using list as a variable name is a bad idea as it is a python builtin. But your list here is a list of words according to the description. Therefore what you want is:
for word in word_list:
    if word[0] == "U":

-Mek
Was This Post Helpful? 2
  • +
  • -

#5 Connectification   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-June 13

Re: Can anyone explain for me why this code didn't work?

Posted 02 June 2013 - 09:24 AM

View PostMekire, on 02 June 2013 - 09:09 AM, said:

Firstly using list as a variable name is a bad idea as it is a python builtin. But your list here is a list of words according to the description. Therefore what you want is:
for word in word_list:
    if word[0] == "U":

-Mek


My last programming language is Java. When I have to do Java, I would usually come up with something like this:

public void sum_list(L):
new_list = []
for (int i = 0; i < L.length; i++):
new_list = new_list.append(L[i])
return sum(new_list)



It kinda a problem for me to get used to that i is no longer an index but an elements in the list. Is there anyway that I can do something similar like that in Python? And about the method that you showed me
for word in word_list:
	    if word[0] == "U":

You meant to tell me to create a nested for loop, right?
Was This Post Helpful? 0
  • +
  • -

#6 Mekire   User is offline

  • D.I.C Head

Reputation: 118
  • View blog
  • Posts: 216
  • Joined: 11-January 13

Re: Can anyone explain for me why this code didn't work?

Posted 02 June 2013 - 09:36 AM

You need to get used to for loops looping over objects. It is the python way. You could do it with indexes but it wouldn't be pythonic. And no I wasn't implying a nested for loop.

Showing you more is the same as showing you the answer so please try to really understand it:

Spoiler

-Mek

Edit: Slight change to short version.

This post has been edited by Mekire: 02 June 2013 - 10:18 AM

Was This Post Helpful? 1
  • +
  • -

#7 Connectification   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-June 13

Re: Can anyone explain for me why this code didn't work?

Posted 02 June 2013 - 09:43 AM

View PostMekire, on 02 June 2013 - 09:36 AM, said:

You need to get used to for loops looping over objects. It is the python way. You could do it with indexes but it wouldn't be pythonic. And no I wasn't implying a nested for loop.

Showing you more is the same as showing you the answer so please try to really understand it:

Spoiler

-Mek


I get it now, Mek. :) Thank you for being so detailed in your response.
Was This Post Helpful? 0
  • +
  • -

#8 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2644
  • View blog
  • Posts: 4,224
  • Joined: 21-June 11

Re: Can anyone explain for me why this code didn't work?

Posted 02 June 2013 - 09:45 AM

View PostConnectification, on 02 June 2013 - 06:24 PM, said:

My last programming language is Java.

Just for the record: If you wrote for(String i : words) in Java, i would be a word, not an index, as well.

Since iterating without an index is generally less complicated than using an index, I don't see why you'd want an index unless you need one (which you don't in this case).
Was This Post Helpful? 2
  • +
  • -

#9 Connectification   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-June 13

Re: Can anyone explain for me why this code didn't work?

Posted 02 June 2013 - 09:46 AM

View PostMekire, on 02 June 2013 - 09:36 AM, said:

You need to get used to for loops looping over objects. It is the python way. You could do it with indexes but it wouldn't be pythonic. And no I wasn't implying a nested for loop.

Showing you more is the same as showing you the answer so please try to really understand it:

Spoiler

-Mek


By the way, i didn't know that you can find out the length of a list in Python without declaring it as a variable first. It's so cool
Was This Post Helpful? 0
  • +
  • -

#10 Connectification   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-June 13

Re: Can anyone explain for me why this code didn't work?

Posted 04 June 2013 - 04:27 AM

View PostMekire, on 02 June 2013 - 09:36 AM, said:

Spoiler

-Mek

Edit: Slight change to short version.


I do not quite understand your revise version. Can you explain it a little bit more for me, please?
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw   User is offline

  • Entwickler
  • member icon

Reputation: 6602
  • View blog
  • Posts: 26,897
  • Joined: 12-December 12

Re: Can anyone explain for me why this code didn't work?

Posted 04 June 2013 - 05:44 AM

He is summing 1 for every word that begins with "U" - which is the same as counting.
Was This Post Helpful? 1
  • +
  • -

#12 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11224
  • View blog
  • Posts: 19,242
  • Joined: 19-March 11

Re: Can anyone explain for me why this code didn't work?

Posted 04 June 2013 - 06:32 AM

View PostMekire, on 02 June 2013 - 11:36 AM, said:

You need to get used to for loops looping over objects. It is the python way. You could do it with indexes but it wouldn't be pythonic.



And more important, it's a less expressive way to write a program. You're not interested in the indexes of the list, you're interested in the members of the list.

for word in words:
  do_something_to(word)



is easier to understand and to reason about than

for i in range (len (words)):
  do_something_to(words[i])



I went through the same thing when I started learning python a year ago - it'll be confusing for a little while, but you'll get the hang of it.
Was This Post Helpful? 1
  • +
  • -

#13 Connectification   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-June 13

Re: Can anyone explain for me why this code didn't work?

Posted 05 June 2013 - 08:03 PM

View Postandrewsw, on 04 June 2013 - 05:44 AM, said:

He is summing 1 for every word that begins with "U" - which is the same as counting.
def measure_udacity(word_list):
    return sum(1 for word in word_list if word[0] = "U"



Oh. I thought that the reserved word sum only can be used explicitly to calculate the sum of the list or tuple, given that the list and tuple had been declared before. Never thought that you can do something like that
Was This Post Helpful? 0
  • +
  • -

#14 Connectification   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-June 13

Re: Can anyone explain for me why this code didn't work?

Posted 05 June 2013 - 08:13 PM

View Postjon.kiparsky, on 04 June 2013 - 06:32 AM, said:

View PostMekire, on 02 June 2013 - 11:36 AM, said:

You need to get used to for loops looping over objects. It is the python way. You could do it with indexes but it wouldn't be pythonic.



And more important, it's a less expressive way to write a program. You're not interested in the indexes of the list, you're interested in the members of the list.

for word in words:
  do_something_to(word)



is easier to understand and to reason about than

for i in range (len (words)):
  do_something_to(words[i])



Another way that you can go through the list using index
while i < len(words)
    do_something_to(words[i])
i++


Was This Post Helpful? 0
  • +
  • -

#15 code_m   User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 202
  • Joined: 21-April 09

Re: Can anyone explain for me why this code didn't work?

Posted 05 June 2013 - 08:29 PM

View PostConnectification, on 05 June 2013 - 11:03 PM, said:

View Postandrewsw, on 04 June 2013 - 05:44 AM, said:

He is summing 1 for every word that begins with "U" - which is the same as counting.
def measure_udacity(word_list):
    return sum(1 for word in word_list if word[0] = "U"



Oh. I thought that the reserved word sum only can be used explicitly to calculate the sum of the list or tuple, given that the list and tuple had been declared before. Never thought that you can do something like that


sum does work with lists, you are correct. However, you are missing that the statement is a "list comprehension". Do a bit of reading on this :) (or play around in the python interpreter, it's really great!)
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2