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:
-
You define a function named while_loop
with two parameters start
and end
.
-
You define a empty list called numbers inside the function.
-
Yo define a while loop inside the function with a start- and a endpoint.
-
Increment the start by one.
-
Append the actual number to the list.
-
return the list numbers
.
-
define a variable make_numbers
with a function call of while_loop
with a startpoint of 5 and a endpoint of 20.
-
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
zedshaw
#5
That’s good enough. Keep going. Also remember what other said about how to format code:
[code]
# your code here
[/code]
1 Like