Ex33 study drill 5


#1

I’m working through the study drills for ex33 and when I call this function nothing prints in Powershell

def for_loop(start, end, increment):
	number = []
	for x in range(start, end, increment):
		number.append(x)
		return number
	for x in number:
		print(number)

for_loop(0, 100, 5)

For peace of mind, if anyone has any comments on my attempt at drill 3 I’d appreciate it :slight_smile: :

 def nummer(nums):
	numbers = []
	i = 0
	inc = int(input("enter the value of increment you require: "))
	while i < nums:
		print(f"At the top, i is {i}")
		numbers.append(i)
		i += inc
		print("Numbers now: ", numbers)
		print(f"at the bottom, i is {i}\n")
		print("The numbers: ")
	for num in numbers:
		print(num)

Thank youuu


#2

I think your issue is that you are returning control from your for_loop() before the print statement (and second loop), so it is not reached.

I’m not 100% on what you are trying to achieve but I doubt you need the second loop. You could print in the defined function that will print each iteration rather than return it.

Or if you want to print the final output (after it is returned from the function), you could just call print on the function itself. Notice the return function is outside of the loop. You want to return the list completed after the loop has run, I assume.

Print iterations:

def for_loop(start, end, increment):
	number = []
	for x in range(start, end, increment):
		number.append(x)
		print(number)

for_loop(1, 100, 5)

Print final list:

def for_loop(start, end, increment):
    number = []
    for x in range(start, end, increment):
		number.append(x)
    
    return number

print(for_loop(1, 100, 5))

#3

Thank you, that makes a lot of sense!