Last answered:

08 Aug 2022

Posted on:

05 Aug 2022

0

Resolved: Why is the reverse method not working in question 2?

This is the code I'm using:
    "inp=input('Please name your favorite food:>')
lis=[]
for n in (inp):
    lis.append(n)
    print(lis.reverse)"

and here is the output i'm getting:

image.png

1 answers ( 1 marked as helpful)
Instructor
Posted on:

08 Aug 2022

1

Hey,

Thank you for your question!

Notice that, in your code, the print-function is inside the for-loop. Therefore, each time you append a letter to lis, the program prints out the output of lis.reverse. Since the word 'steak' contains 5 letters, the message is printed out 5 times. Additionally, what you print out is not the reversed list but rather a message describing what the reverse() method is - namely, a built-in method of a list object.

What I provide below is a modification of the code, such that it returns a string with the reversed name of the food. Notice that on line 10 the reverse keyword is followed by a set of parentheses.
image.png

Hope this helps!

Kind regards,
365 Hristina

Submit an answer