Q. Assuming words is a valid list of words, the program below tries to print the list in reverse. Does it have an error? If so, why?


(Hint. There are two problems with the code.)

for i in range(len(words), 0, -1) :
    print (words[ i ] , end =' ')


Answer :-

Yes, there is an error.
there should be -1, not 0.

Correct program -

words = eval(input( "Enter a list of Words :- " ))
for i in range(len(words)-1, -1, -1) :
    print (words[ i ] , end =' ')

Output :-

Enter a list of Words:- [ "Path","Walla","Portal","Express"]
Express Portal Walla Path 
>>>

12 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. This correct code also giving error

    ReplyDelete
    Replies
    1. because you have not define words variable.

      Delete
  2. According to the question, words is a list of string but in above mentioned correct program, words is just a string. So I guess the program is wrong

    ReplyDelete
  3. It should be
    (len(words)-1,-1,-1)

    ReplyDelete
    Replies
    1. no you are wrong because you did not give the for loop

      Delete
  4. Thanks for all solutions ❤️

    ReplyDelete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post