How to sort 10 integers in python?
Question:
I am a novice programmer and have been struggling with some simple problems for too long
The pictures are sample details
Answers:
It depends if you can use built-in functions of python such as sort().
If you can then you need an input to get all the values from the user. Apparently you are struggling on sorting them instead of retrieving them, in this case it is better to store them into lists (so your data looks like [4, 85, 3, 234, 45, …]).
Then you can use the sort() function to sort the list.
After that you can do a
for in in range(len(listName)):
print(listName[i]
And you will have the output asked.
Assuming you’re taking user (keyboard) input, then you could do this:
indata = input('Enter some integers separated by space: ')
print(*sorted(map(int, indata.split())), sep='n')
Output:
Enter some integers separated by space: 4 85 3 234 45 345 345 122 30 12
3
4
12
30
45
85
122
234
345
345
What this does…
Gets a string from the standard input. Split the string into tokens delimited by whitespace. Convert each token to an int. Sort the output from the map() object and print
I am a novice programmer and have been struggling with some simple problems for too long
The pictures are sample details
It depends if you can use built-in functions of python such as sort().
If you can then you need an input to get all the values from the user. Apparently you are struggling on sorting them instead of retrieving them, in this case it is better to store them into lists (so your data looks like [4, 85, 3, 234, 45, …]).
Then you can use the sort() function to sort the list.
After that you can do a
for in in range(len(listName)):
print(listName[i]
And you will have the output asked.
Assuming you’re taking user (keyboard) input, then you could do this:
indata = input('Enter some integers separated by space: ')
print(*sorted(map(int, indata.split())), sep='n')
Output:
Enter some integers separated by space: 4 85 3 234 45 345 345 122 30 12
3
4
12
30
45
85
122
234
345
345
What this does…
Gets a string from the standard input. Split the string into tokens delimited by whitespace. Convert each token to an int. Sort the output from the map() object and print