Duplicate Question
The question on this page has been marked as a duplicate question.
Original Question
The program below initializes a list with integers between two user-entered numbers. It should then traverse or iterate through...Question
The program below initializes a list with integers between two user-entered numbers. It should then traverse or iterate through the list backward, printing out each element value on the same line. The user input, list creation, and final print() statement are done for you. Take the following steps where shown by the comments.
Add a "for" loop with the "in" keyword and a range to traverse the values list backward (from the last element to first).
Inside the loop, print the current element value on the same line, separated by a space.
The following examples demonstrate the expected program output.
Enter starting list value: 2
Enter ending list value: 5
5 4 3 2
Backward traversal complete
Enter starting list value: 0
Enter ending list value: 20
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Backward traversal complete
values = []
start = int(input("Enter starting list value: "))
end = int(input("Enter ending list value: "))
for i in range(start,end+1):
values.append(i)
#student code here
print("\nBackward traversal complete")
Add a "for" loop with the "in" keyword and a range to traverse the values list backward (from the last element to first).
Inside the loop, print the current element value on the same line, separated by a space.
The following examples demonstrate the expected program output.
Enter starting list value: 2
Enter ending list value: 5
5 4 3 2
Backward traversal complete
Enter starting list value: 0
Enter ending list value: 20
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Backward traversal complete
values = []
start = int(input("Enter starting list value: "))
end = int(input("Enter ending list value: "))
for i in range(start,end+1):
values.append(i)
#student code here
print("\nBackward traversal complete")
Answers
There are no human answers yet.
Answered by
GPT 3.5
values = []
start = int(input("Enter starting list value: "))
end = int(input("Enter ending list value: "))
for i in range(start,end+1):
values.append(i)
#student code here
for i in range(len(values)-1, -1, -1):
print(values[i], end=" ")
print("\nBackward traversal complete")
start = int(input("Enter starting list value: "))
end = int(input("Enter ending list value: "))
for i in range(start,end+1):
values.append(i)
#student code here
for i in range(len(values)-1, -1, -1):
print(values[i], end=" ")
print("\nBackward traversal complete")
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.