Exercise 33 Study Drill 3 & 4

Is this how Zed wants us to use a variable in study drill 3? I think it’s too simple so I wonder if there is something else to it.

numbers = []

def while_loop(limit):
i = 7
while i < limit:
numbers.append(i)
add = 3
i += add
return numbers

numbers = while_loop(100)
print(numbers)

If you put your code in square bracket tags, it will format and be easier to read

(code)
Some code
(/code)

(Change parentheses with [ ] )

1 Like

Your code produces the output 7:

numbers = []

def while_loop(limit):
   i = 7
   while i < limit:
      numbers.append(i)
      add = 3
      i += add
      return numbers

numbers = while_loop(100)
print(numbers)

The question is, if that’s intended by you?

I think you want something like this:

def while_loop(start, end):
    numbers = []
    while start < end:
        start += 1
        numbers.append(start)
    return numbers

make_numbers = while_loop(5, 20)
print(make_numbers)

Step by step:

  1. You define a function named while_loop with two parameters start and end.

  2. You define a empty list called numbers inside the function.

  3. Yo define a while loop inside the function with a start- and a endpoint.

  4. Increment the start by one.

  5. Append the actual number to the list.

  6. return the list numbers.

  7. define a variable make_numbers with a function call of while_loop with a startpoint of 5 and a endpoint of 20.

  8. print the variable make_numbers

Output:
[6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Hope that helps.

2 Likes

That’s good enough. Keep going. Also remember what other said about how to format code:

[code]
# your code here
[/code]
1 Like