Question: Write a function that takes a string as an argument and outputs the le
ID: 3619691 • Letter: Q
Question
Question: Write a function that takes a string as an argument and outputs the letters backward, one per line. ----- Below is the reading material that goes with the above question. ----- Traversal and the for loopA lot of computations involve processing a string one character at a time. Often
they start at the beginning, select each character in turn, do something to it, and
continue until the end. This pattern of processing is called a traversal. One way
to encode a traversal is with a while statement:
index = 0
while index < len(fruit):
letter = fruit[index]
print letter
index = index + 1 This loop traverses the string and displays each letter on a line by itself. The
loop condition is index < len(fruit), so when index is equal to the length of
the string, the condition is false, and the body of the loop is not executed. The
last character accessed is the one with the index len(fruit)-1, which is the last
character in the string. Question: Write a function that takes a string as an argument and outputs the letters backward, one per line. ----- Below is the reading material that goes with the above question. ----- Traversal and the for loop
A lot of computations involve processing a string one character at a time. Often
they start at the beginning, select each character in turn, do something to it, and
continue until the end. This pattern of processing is called a traversal. One way
to encode a traversal is with a while statement:
index = 0
while index < len(fruit):
letter = fruit[index]
print letter
index = index + 1 This loop traverses the string and displays each letter on a line by itself. The
loop condition is index < len(fruit), so when index is equal to the length of
the string, the condition is false, and the body of the loop is not executed. The
last character accessed is the one with the index len(fruit)-1, which is the last
character in the string.
Explanation / Answer
def printBackwards(word):index = len(word) - 1
while index >= 0:
letter = word[index]
print letter
index = index - 1
var = "test"
printBackwards(var)
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.