Correcting my code

Hey guys, i am having trouble correcting my code for this question

Input 7 positive numbers and make each number negative. Then add the negative number to a list.
Print the list of negative numbers in the reverse order it was entered.
Print the word “End” before the program ends.

i have entered this code but it does not seem to work.
my_list=
for i in range(7):
num=int(input("Enter a number: "))
my_list.append(-num)

print(list(reversed(1st)))
print(“End”)

Would really appreciate if someone helps me

You’ve got at least one SyntaxError in your program. Hint: read the error message you get carefully and check all your variable names.

One more note: putting your code between triple backquotes like “```” will preserve indentation and make it easier for people to help you. For example, your code would look like:

my_list= []
for i in range(7):
    num=int(input("Enter a number: "))
    my_list.append(-num)

print(list(reversed(1st)))
print("End")

You can (and should) ask ‘Python’ for help, but I’m guessing that nobody has told you about that.

As an example: you want to know how to “reverse” or “sort” a list object (maybe you can “sort” in reverse order, right?).

If you use the REPL interface (AKA: the ‘Python shell’), you get a welcome message and >>> prompt. As an example, if I open a terminal session and start the Python3 REPL, this is what I see:

:~$ python3
Python 3.6.9 (default, Mar 10 2023, 16:46:00) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Now I create a list object and ask ‘Python’ for some help:

>>> my_list = [1,2,3,4]
>>> help(my_list)

Now I see quite a lot of ‘help’ and right there, near the end (use the arrow down key, or Pg Dn key) I see:

 |  reverse(...)
 |      L.reverse() -- reverse *IN PLACE*
 |  
 |  sort(...)
 |      L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*

Once you’re done reading, you simply press the ‘q’ key, to quit the help system.

So, we can now see that there are two ways that we can achieve the objective; both of which are *IN PLACE*, which means that the list object will be remain that way, unless we perform an operation that alters it.

You should now be able to see that:
my_list.reverse()
or:
my_list.sort(reverse=True)
…will do what we need to do.

I hope this ‘mini guide’ is of some use to you.

1 Like

thanks for the mini guide Rob, one more question, how do you reverse a list, i have tried multiple ways but i cant seem to reverse my list to get the correct answer

As I said:

So, to apply what I said, to your code:

my_list = []
for i in range(7):
    num = int(input("Enter a number: "))
    my_list.append(-num)

print("The list as entered:", my_list)
my_list.reverse() # this is the the point of my response and the principle that you need.
print("The list reversed:", my_list)
print("End")

I trust that this will clarify the post for you.

If you simply want to display the elements of a list, rather than the list object, you can use print(*my_list). Try that, so that you can see what the difference is.

This is answered in the guide. It rings hollow to “thank” people if you do not consider the information they have already given you. If you read it and encountered a problem with the explanation, then you should show how you tried to use that information: i.e., show the new code and show clearly what went wrong when you tried it.

Please also read the pinned thread and make sure you understand how to post code with proper formatting, so that we can properly see how you have typed it.

We can only possibly help understand what went wrong with code, if you actually show that code. Saying that you “tried multiple ways” is not helpful, because it does not let us see any of those ways. It comes across as simply trying to justify that you deserve the help. That justification isn’t necessary; this is a help forum, after all.

2 Likes