OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

The difference between a "for" loop and a "while" loop

  • Thread starter Thread starter Callilou
  • Start date Start date
C

Callilou

Guest
This is more of a question to aid my understanding as I learn python. I am working my way through an exercise book and the following challenge was given:

Start with a copy of your program from Exercise 8-9. Write a function called send_messages() that prints each text message and moves each message to a new list called sent_messages as it’s printed. After calling the function, print both of your lists to make sure the messages were moved correctly.

The code I wrote initially used a 'for' loop, which didn't iterate through all of the messages: `messaging = ['Hello world!', 'Keep it coming!', "Don't give up!", "I'm getting hungry!"] # list of messages to send sent_messages=[] # list of sent messages def show_messages(messages): #define the name of a list in function **for message in messages: #while loop to keep running until list has been gone through current_mess = messages.pop() #remove latest message print(current_mess) # print latest message sent_messages.append(current_mess) # add latest message to sent_messages

show_messages(messaging[:]) #create a copy of the messaging list so original isn't touched by using a slice print(messaging) print(sent_messages)`

The output for this was as follows:

I'm getting hungry! Don't give up! ['Hello world!', 'Keep it coming!', "Don't give up!", "I'm getting hungry!"] ["I'm getting hungry!", "Don't give up!"]

However when I changed the function to have a "while" loop, it worked correctly: ` messaging = ['Hello world!', 'Keep it coming!', "Don't give up!", "I'm getting hungry!"] # list of messages to send sent_messages=[] # list of sent messages def show_messages(messages): #define the name of a list in function while messages: #while loop to keep running until list has been gone through current_mess = messages.pop() #remove latest message print(current_mess) # print latest message sent_messages.append(current_mess) # add latest message to sent_messages

show_messages(messaging[:]) #create a copy of the messaging list so original isn't touched by using a slice

print(messaging) print(sent_messages)`

Output:

I'm getting hungry! Don't give up! Keep it coming! Hello world! ['Hello world!', 'Keep it coming!', "Don't give up!", "I'm getting hungry!"] ["I'm getting hungry!", "Don't give up!", 'Keep it coming!', 'Hello world!']

I was mainly curious as to what is exactly happening i these two versions of the code to make on work and one not which may improve my understanding of loops.
<p>This is more of a question to aid my understanding as I learn python. I am working my way through an exercise book and the following challenge was given:</p>
<blockquote>
<p>Start with a copy of your program from Exercise 8-9.
Write a function called send_messages() that prints each text message and
moves each message to a new list called sent_messages as it’s printed. After
calling the function, print both of your lists to make sure the messages were
moved correctly.</p>
</blockquote>
<p>The code I wrote initially used a 'for' loop, which didn't iterate through all of the messages:
`messaging = ['Hello world!', 'Keep it coming!', "Don't give up!", "I'm getting hungry!"] # list of messages to send
sent_messages=[] # list of sent messages
def show_messages(messages): #define the name of a list in function
**for message in messages: #while loop to keep running until list has been gone through
current_mess = messages.pop() #remove latest message
print(current_mess) # print latest message
sent_messages.append(current_mess) # add latest message to sent_messages</p>
<p>show_messages(messaging[:]) #create a copy of the messaging list so original isn't touched by using a slice
print(messaging)
print(sent_messages)`</p>
<p>The output for this was as follows:</p>
<blockquote>
<p>I'm getting hungry!
Don't give up!
['Hello world!', 'Keep it coming!', "Don't give up!", "I'm getting hungry!"]
["I'm getting hungry!", "Don't give up!"]</p>
</blockquote>
<p>However when I changed the function to have a "while" loop, it worked correctly:
`
messaging = ['Hello world!', 'Keep it coming!', "Don't give up!", "I'm getting hungry!"] # list of messages to send
sent_messages=[] # list of sent messages
def show_messages(messages): #define the name of a list in function
while messages: #while loop to keep running until list has been gone through
current_mess = messages.pop() #remove latest message
print(current_mess) # print latest message
sent_messages.append(current_mess) # add latest message to sent_messages</p>
<p>show_messages(messaging[:]) #create a copy of the messaging list so original isn't touched by using a slice</p>
<p>print(messaging)
print(sent_messages)`</p>
<p>Output:</p>
<blockquote>
<p>I'm getting hungry!
Don't give up!
Keep it coming!
Hello world!
['Hello world!', 'Keep it coming!', "Don't give up!", "I'm getting hungry!"]
["I'm getting hungry!", "Don't give up!", 'Keep it coming!', 'Hello world!']</p>
</blockquote>
<p>I was mainly curious as to what is exactly happening i these two versions of the code to make on work and one not which may improve my understanding of loops.</p>
 

Latest posts

Top