Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (Retired) Putting the "Fun" Back in "Function" Functions

Strange outcome of addition.

1,

I was trying to figure out, how split units of list and add them together according to our task 1 of 2. Task 1/2

I have tried this:

def a(list):
    for unit in list:
        return unit + unit

b = [4, 2, 5]

print (a(b))

How come number 8 ? o_O

2,

I was googling and there is function for that, but I know we are not supposed to use it yet:

def a(list):
    for unit in list:
        return sum(list)

b = [4, 2, 5]

print (a(b))

3,

So I have to google even more to find out suitable solution. I found this:

def a(list):
    start = 0
    for unit in list:
        start = start + unit
    return start

a([3, 5, 1])

But it does not show any output. Why ? I had to modify it like this:

def a(list):
    start = 0
    for unit in list:
        start = start + unit
    return print(start)

a([3, 5, 1])

4,

How are we supposed to solve it ? 1 of 2 and 2 of 2. I have no idea !

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher
def a(list):
    for unit in list:
        return unit + unit

b = [4, 2, 5]

print (a(b))

How come number 8 ?

Because you're returning after every step in the loop. And the first step is 4 + 4, which equals 8.


def a(list):
    start = 0
    for unit in list:
        start = start + unit
    return start

a([3, 5, 1])

But it does not show any output. Why ?

Because return doesn't show output unless you're running in the Python shell. This is exactly what your solution should be for the code challenge. No printing in code challenges!

Does not work with both indentations. o_O

pic pic

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

It's not the indentation, it's the code that isn't passing. You're returning unit every time, not start, which is what ends up being your total amount.

Ah y sorry.

I have made it, even 2 of 2. But then like all the time, appeared that congratulation pop up with +points, and I lost chance to copy paste code away or look closer to code. It's stupid, because now I can't get back to that original version I have written into that window. I have accomplished it, but my input was not remained.

I want go back to it, because code i passed in, I was just trying, I'm not 100% sure I understand it !!!

It is super dumb that it always dissapear, there should be some button to Show/Hide my solution. For future. We aren't elephants that remember everything~~!

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

In Safari, at least, hitting Esc will close the points modal and you'll be able to mess with the code again. Not sure about any other browsers.

I'm using Mozilla Firefox. Esc works. But what about next day ? It will be gone

Not fair

Ivรกn Higuera-Mendieta
Ivรกn Higuera-Mendieta
764 Points

Hello to all,

I am having problems with the second part of the Challenge, which involves the creation of summarize function. I am trying the following code, but the app keeps me saying "Oops! It looks like Task 1 is no longer passing".

def add_list(list):
  add = 0
  for item in list:
    add = add + item
  return add

def summarize(list):
  add = 0
  for item in list:
    add = add + item
  return add
  print("The sum of {} is {}".format(', '.join(list), add)

I also tried this, but the outcome was the same

def add_list(list):
  add = 0
  for item in list:
    add = add + item
  return add

def summarize(list)
    print("The sum of {} is {}".format(', '.join(list), add_list(list))

What am I doing wrong, this task is horrible :'(

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

summarize() needs to return the string, not print() it.