In: Computer Science
Hello,
***We have to print the second list backward while interleaved with the first list in normal order.***
its PYTHON language.
Please refer the below example. Bold are the inputs that the user enters. In here the user input words and much as he wants and when he enter STOP, it stops and he is asked to enter more words.(second words count equals to the word count he inputs in the first phase)
Enter words, STOP to stop. Ok
Enter words, STOP to stop. we're
Enter words, STOP to stop. this
Enter words, STOP to stop. STOP
Ok. Now enter 3 other words.
Word 0: again?
Word 1: doing
Word 2: wait
Now I will magically weave them more weirdly!
Ok
wait
we're
doing
this
again?
PS : I have done it actually but the I have a problem getting the last output.(how to merge the 2 lists.) It only prints the words in input order.
MY CODE :
firstlist = [] c=0 stop = False while not stop: a = input("Enter words, STOP to stop. : ") if a == "STOP": stop = True else: c+=1 firstlist.append(a) print("Okay. Now enter other " + str(c) +" numbers!") for i in range(c): v = input('Enter word ' + str(i+1) + ': ' ) firstlist.append(v) print("I'm going to magically weave the words!!") for x in firstlist: print(x)
Please help.
firstlist = []
c=0
stop = False
while not stop:
a = input("Enter words, STOP to stop. : ")
if a == "STOP":
stop = True
else:
c+=1
firstlist.append(a)
print("Okay. Now enter other " + str(c) +" numbers!")
secondlist = [] # TAKE a new list
for i in range(c):
v = input('Enter word ' + str(i+1) + ': ' )
secondlist.append(v) # put elements in that new list
print("I'm going to magically weave the words!!")
# loop for c times
last = c-1
for i in range(c):
print(firstlist[i]) # print first list from first
print(secondlist[last]) # print second list from last
last = last - 1 # decrement index of second list by 1
''' OUTPUT
Enter words, STOP to stop. : Ok
Enter words, STOP to stop. : We're
Enter words, STOP to stop. : This
Enter words, STOP to stop. : STOP
Okay. Now enter other 3 numbers!
Enter word 1: again?
Enter word 2: doing
Enter word 3: what
I'm going to magically weave the words!!
Ok
what
We're
doing
This
again?
'''
# hit the thumbs up if you are happy!