How to print a line back with a for loop

I need to create a program that allows the user to enter a line, and then, using a range, the program displays the same line back. I introduced this in python, but in my fourth line this is an error. 'int' object has no attribute '__getitem__'. Can someone please help me fix this? (Using range)

user=raw_input("Please enter a string: ")
user1=len(user)-1
for user in range(user1,-1,-1):
    user2=user[user1]
    print user2
+4
source share
5 answers

I think you have a mistake because you continue to use the same words to describe very different types of data. I would use a more descriptive naming scheme:

user = raw_input("Please enter a string: ")
user_length = len(user)
for string_index in range(user_length - 1, -1, -1):
    character = user[string_index]
    print(character)

For example, if there was user input foo, it returned:

o
o
f
+3

for-loop, , for-loop

Fix:

for ind in range(user1,-1,-1):
    user2 = user[ind]
    print (user2)

( ):

print user[::-1]
0

, user int for user in range(...)

You might be better off:

user=raw_input("Please enter a string: ")
for user1 in range(len(user)-1,-1,-1):
    user2=user[user1]
    print user2
0
source

yours has userbeen overwritten in your for loop. Take it (range of use)

user=raw_input("Please enter a string: ")
print ''.join([user[i] for i in range(len(user)-1, -1, -1)])
0
source

Python 3 solution:

user=input("Please enter a string: ")
for ind in range(1,len(user)+1):
    char = user[-ind]
    print(char)

And another solution for non loop:

''.join(reversed(user))
0
source

All Articles