0
How do I receive 10 integers and receive them separated by spaces in the same row in the list and print them in reverse order.
I remember it was something like this:
variavelA, variavelB = input().split(' ')
0
How do I receive 10 integers and receive them separated by spaces in the same row in the list and print them in reverse order.
I remember it was something like this:
variavelA, variavelB = input().split(' ')
1
With the following entry: 1 2 3 4 5 6 7 8 9 0
The following output is obtained:
0
9
8
7
6
5
4
3
2
1
With the following code:
[print(x) for x in input().split()[::-1]]
And a reverse list ['0', '9', '8', '7', '6', '5', '4', '3', '2', '1']
with input().split()[::-1]
How would that code look without using the FOR?
1
Starting from the premise that the user will type the numbers separated by spaces, that is, since it has to be on the same line, it is not possible to check each typing (you could do the checking after typing)but, abstracting this check you could do everything a single command line:
print(' '.join((input('entre com os numros: ').split(' '))[::-1]))
If the user logs in 10 20 30 40 50 60 70 80 90 100
, you would have the exit:
100 90 80 70 60 50 40 30 20 10
Separately explaining:
Explaining each part of the control from the center to the ends.
input('entre com os numros: ')
:
Receives the user’s typing into an object in memory, string type;
.split(' ')
:
Create a list from the typed string, where each element is the substring immediately preceding the space.
[::-1])
Make a Slice by inverting the list.
' '.join()
Converts the list to a string back.
print()
Displays the result
Implementing separately:
The same commands executed on the single line above, now implemented separately:
# Inputando os números
numbers = input('entre com os numeros: ')
# Convertendo para uma lista:
lst_numbers = numbers.split(' ')
# Invertendo a lista
lst_inverted = lst_numbers[::-1]
# Convertendo a lista invertida para uma string
str_inverted = ' '.join(lst_inverted)
# Apresentando o resultado
print(str_inverted)
Exit:
'100 90 80 70 60 50 40 30 20 10'
lst_numbers = `.split(' ') is with error of identation.
@Alexfeliciano In fact it was not in the indentation, I copied from the top and ended up forgetting to put the original string numbers
, edited and corrected, see if funf now.
In your code, why doesn’t the result look like this ['100', '90', '80', '70', '60', '50', '40', '30', '20', '10']
as a list, and instead gets 100 90 80 70 60 50 40 30 20 10
So how? I don’t understand.
In run.codes he gives as incorrect because he wants the results inside a list this way ['100', '90', '80', '70', '60', '50', '40', '30', '20', '10']
You are presenting the str_inverted
, that is, the inverted string, to present the inverted list you must do: print(lst_inverted)
, or could suppress the last 2 commands
It worked thanks, it’s just that I forgot it was to print inversely
Browser other questions tagged python
You are not signed in. Login or sign up in order to post.
You do not need to assign in two variables; as you put in the title yourself, use a list. The return of
split
will be a list, assigning it to a variable only you can use thesorted
orsort
to classify it the way you wish. Remember thatinput
always returns a string, then your list will be strings, not numbers. do the conversion before ordering. Want to try?– Woss
How do you do this using the list in Python?
– user141036
Read multiple numbers on the same line with raw_input | Inverting sequence (PYTHON 3)
– Woss