0
Help reversing a int list in python
X = input() Y = X.split() for i in range(len(y)): k = [::-1] k[i]= int(k[i]) print(k) Prints the list converts it to int but doesnât reverse it help
17 Answers
+ 5
Whenever the user inputs the number via input() it will be a ***string***, not an integer in a list.
Observe the difference, when you put:
x = '12345' # input()
y = [int(i) for i in x]
print(y[::-1])
If you need help, you need to give the complete task description (+ course name).
+ 3
3 options of many here
x = input().split() # makes your list
y = [int(i) for i in x]
print(y[::-1])
#or
b = []
for i in x:
b.append(int(i))
print(b[::-1])
#or
c = []
for i in range(len(x),0,-1):
c.append(i)
print(c)
+ 2
X33X
Try separating your numbers with a space, else the code reads it as a single number
+ 2
Ipang
Good suggestion, else we gonna capsize soon. đ€Łđ
+ 2
I sure hope not Rik đ
+ 1
For this <X> input
"12 23 34 45 56"
Is the result supposed to be like this?
[ 56, 45, 34, 23, 12 ]
Or this?
[ 21, 32, 43, 54, 65 ]
Or this?
[ 65, 54, 43, 32, 21 ]
Or something else?
+ 1
X33X
How are you inputting your numbers.
if you put them in as: 1 2 3 4 5, then the codes will work.
if you input as 1,2,3,4,5, then you will need x = input.split(',')
Let me know how you go, if you still can't get it, then I will write an example for you
+ 1
X33X,
Better post a link to the code in progress, so we can all stay in the same boat.
+ 1
Thanks that totally fixed it lol guess we didnt need to convert the str to a list first
https://code.sololearn.com/cQ8nA3DZ7SvD/?ref=app
0
You can use the reverse() method on the list.
0
I tried the reverse method on my code i still got the exact same input in int mot reversed
For the X input the result should be a reversed list of the input e.g [1,2,3,4] = [4,3,2,1]
And i tried all three options
x = input().split() # makes your list
y = [int(i) for i in x]
print(y[::-1])
Output = the same input unreversed e.g [1,2] is still [1,2]
b = []
for i in x:
b.append(int(i))
print(b[::-1])
Output= no output.
or
c = []
for i in range(len(x),0,-1):
c.append(i)
print(c)
Output = e.g input = [1,2,3] it prints [â123â, 1]
None worked dont know whats wrong
0
My input is 12345 i didnât separate my input with commas
0
Putting spaces in between the number works but the project sepcifies the user input all number without spaces its the credit card validator on sololearn
It should read it all as a str first the we can reverse the str and turn that into an int (that was my idea)
0
y should be of the same case eg both should either be of capital leter or small letter and not mixing
0
Do nothing ; just âlist_name.reverseâ
0
X =[1,2,3,5,3 8,4]
Print(X.reverse())