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 :- " ))
Correct program -
words = eval(input( "Enter a list of Words :- " ))
for i in range(len(words)-1, -1, -1) :
print (words[ i ] , end =' ')
print (words[ i ] , end =' ')
Output :-
Enter a list of Words:- [ "Path","Walla","Portal","Express"]
Express Portal Walla Path
>>>
hi
ReplyDelete😊
DeleteThis correct code also giving error
ReplyDeletebecause you have not define words variable.
DeleteAccording 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
ReplyDeleteOk, I have corrected it.
DeleteIt should be
ReplyDelete(len(words)-1,-1,-1)
No. you are wrong
Deleteno you are wrong because you did not give the for loop
DeleteThanks for all solutions ❤️
ReplyDeleteWelcome : )
DeleteHello
ReplyDeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )