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

Why isn't this working?

When I run the script it won't return anything, it won't even return to the treehouse:~/workspace$

This is the code:

my_nums = []

def adding(my_nums):
    counter = len(my_nums)
    record = 0
    previous = 0
    while record < 32500:
        number = 2*counter
        my_nums.append(previous + number)
        record = record + number
        previous = number

    print(my_nums)

adding(my_nums)

2 Answers

At the top of your code

my_nums = []

You are giving your variable my_nums an empty list. Python then goes through your program and comes across the adding function! The empty list is then passed into the function...

Since the argument (what you passed into adding) is an empty list, the value of counter will be 0 because the length of my_nums is zero.

See if you can follow from that, if you need any more help just ask :)

Good Luck!

Hi Tobias, thanks for your response. I still can't get it. Since counter is zero, two times zero will result in zero, which should then be appended to my_nums list thus increasing my counter in 1. Is my way of thinking wrong?

I thought I might as well post my response as an answer because it's quite long:

So if we were to walk through the program:

  • my_nums (an empty list) is passed into the adding function
  • counter = 0 since the length of my_nums is 0

  • Now we enter the infinite while loop: while record < 32500
  • Inside the loop, number = 0 because counter is 0
  • my_nums will keep appending the number 0 to its list
  • record = itself + 0 (the value of number)
  • previous will equal 0 because number is zero

  • THE PROBLEM: the code inside your while loop is infinite because record will always equal 0 and never be above 32500. In all your variable assignments 0 is being assigned every time.

Due to the infinite loop this is why the program crashes :(

So what you need to try and do is increase the value of record after each iteration of your while loop so that the loop eventually ends!

Can I ask what you're trying to get the program to print at the end? I may be able to help :)

I hope that helps sorry