Ex 38 - LPTHW - how to use for loop instead of while loop for this exercise?

I need help with writing the following code with ‘for loop’ instead of ‘while loop’.

ten_things = “Apples Oranges Crows Telephone Light Sugar” print(“Wait there are not 10 things in that list. Let’s fix that.”)
stuff = ten_things.split(’ ')
more_stuff = [“Day”, “Night”, “Song”, “Frisbee”, “Corn”, “Banana”, “Girl”, “Boy”]

while len(stuff) != 10:
next_one = more_stuff.pop()
print("Adding: “, next_one)
stuff.append(next_one)
print(f"There are {len(stuff)} items now.”)

print("There we go: ", stuff)
print(“Let’s do some things with stuff.”)
print(stuff[1]) 20 print(stuff[-1]) # whoa! fancy!
print(stuff.pop())
print(’ ‘.join(stuff)) # what? cool!
print(’#’.join(stuff[3:5])) # super stellar!

Hello @Mansi13

I guess this should be enough for you to find out how to make the for-loop.
Klick this Link

2 Likes

Format is kinda confusing, but from the looks of it you can write a range based for loop;

For i in range(len(stuff)):
#Stuff[i] returns the items,
#i returns the iterator/index—loop number

To copy your syntax exactly;
For i in range(10):

*keep in mind what the range()’s output, and that len()’s sometimes dont have a respective index item (bc they start at 0). I made errors on these for some time.
Hope this helps.

Zach

2 Likes

for stuff in more_stuff: