What Went Wrong with My Messaging Function?

0
12
Asked By CodeCrafter88 On

Hey folks! I've been learning to code using a book by Eric Matthes, and I'm trying to implement a feature that manages text messages. I've created a function called `show_messages()` to display messages. Now, I need to build another function, `send_messages()`, to take messages from one list and move them to another called `sent_messages()`. This is my latest attempt at writing the function:

```python
def send_messages(finished_messages, unfinished_message):
"""A function that outputs text messages and transfers them to the sent_messages list."""
while unfinished_message:
current_message = unfinished_message.pop()
print(f"Printing current message {current_message}")
finished_messages.append(current_message)

def show_completed_message(finished_messages):
"""Display all completed messages."""
print("nThe following message has been finished:")
for finished_message in finished_messages:
print(finished_message)

unfinished_message = ['Hello']
finished_message = []

send_messages(unfinished_message, finished_message)
show_completed_message(finished_message)
```

Can anyone help me figure out what I might be doing wrong? I would really appreciate any guidance! Thanks in advance!

5 Answers

Answered By SyntaxSorcerer On

You didn’t really specify what "not working" means. Are you not getting any output, or is it giving you errors or unexpected results? Clarifying that could help others give more specific advice.

Answered By CodeyMcCodeface On

Be careful with mutating inputs; it often leads to tricky bugs. Instead of altering your input lists, consider saving your results as separate lists and returning them. Functions should ideally take fewer arguments and yield clean outputs. Here's a quick snippet you might find helpful:

```python
def send_messages(messages):
sent_messages = []
for message in messages:
print(f"Sending message '{message}'")
sent_messages.append(message)
return sent_messages
```

Answered By DebuggingNinja On

It looks like you might have swapped the `finished_messages` and `unfinished_message` arguments in your `send_messages()` function. You should be passing the list that holds messages yet to be sent as the first argument and the list for finished messages as the second one! Try switching them around when you call `send_messages()`.

Answered By TechnoWhiz On

To really see where things are going wrong, step through your `send_messages` function using a debugger. It allows you to observe how your variables change in real time and can quickly reveal the source of the issue.

Answered By ConfusedProgrammer On

To troubleshoot, think about what 'not working' actually looks like. Are you seeing no output, or an error message? If it’s an error, let us know the type and where it pops up. That will help pinpoint the issue better!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.