Write a program that
1. Prompts the user for input of a string
2. Uses for loop to loop through each character of string and inserting each character to the beginning of a list which starts out as empty list (Note: a string can be viewed as a list of characters/letters).
3. Uses for loop to loop through each character of the inserted list and prints out each character/letter so that the input string from step 1. is now print out in reverse. (Note: to print multiple print statements without newline character between them, just pass in a 2nd named parameter of end="" which tells it to not end a line with newline character).
s = input("Enter a string:")
l = []
#loop through each letter in string s and
#keep inserting to beginning of list l
for letter in s:
l.insert(0,letter)
#loop through each letter in l and printing it out
for letter in l:
print (letter,end="")
Tim,
I could not use For Loop.
I had trouble understanding what the problem was asking for.
Besides understanding the logic of the exercise, I also had to understand the text written in English.
k=input("Enter your name"); print("")
x=(len(k)-1)
while x <= len(k) and x>=0:
print(k[x], end="")
x-=1
Tim, thanks for the fun !!
"I feel that in both art and music, it's not the success that matters but the pleasure it gives you. Focus on the pleasure and the learning will come naturally." - Brian Weston
As long as you're able to come up with solution then I guess you've learned something.
While loops are fine as an alternative.
If the question is simply to reach the final result ... yes, I learned a lot from this challenge.
But if the question is didactic, the ends do not justify the means.
I wasn't understanding the For Loops, and I think the exercise above all else is to better explore lesson theory.
I managed to come up with a result. I do not know if that is what was really requested.
It's time to see the Spoiler and find out the easiest way to do it!
words=input("Enter a text")
listt=[]
for word in words:
listt.append(word)
print(listt)
print("*"*50)
listt.reverse()
print(listt)
Thx a lot Tim!
"I feel that in both art and music, it's not the success that matters but the pleasure it gives you. Focus on the pleasure and the learning will come naturally." - Brian Weston
words = ['0','1','2','3','4','5']
x=len(words)-1
listt=[]
for word in words:
print(word, " tracking.word")
listt.insert(x,word)
print(x,word, " tracking.X.Word")
print(listt, " tracking.list")
x-=1
print(" ")
print("*"*50)
print(words, " Initial entry, no change")
print("*"*50)
print(listt, " Result, the order should have been reversed. But not.")
I was using a counter to try to determine the input of the variable at a given position.
Then by chance, I discovered the .reverse (), and threw in the towel.
Then, when I checked the answer, I understood that the Index should be fixed at position 0 (zero), and that with the new entries, it would be pushed to the right.
Even though I failed the challenge using For Loops, I learned a lot on the way.
"I feel that in both art and music, it's not the success that matters but the pleasure it gives you. Focus on the pleasure and the learning will come naturally." - Brian Weston