-3

I know this doubt looks quite easy but i just cant get my head to wrap around it. I was just fooling around with python sorting, when i noticed this.

If I gave in the input as 21 ,8, 4. The output i would receive would be 21, 4,8. Not 4,8,21. What i am assuming is that python is only taking the first digit and comparing, but that is not what i want. How do i fix this problem?

lst=list()
i=0

while i < 3:
    number=input("Give me a number: \n")
    lst.append(number)
    i=i+1
    print("\n")

lst.sort()

print("The list of ordered numbers are:")
for j in lst:
    print(j)

Terminal output

Prathamesh
  • 1,064
  • 1
  • 6
  • 16
Amogh BIju
  • 23
  • 1
  • 5
  • 1
    You're sorting *strings*. – jonrsharpe Jun 12 '20 at 07:12
  • Does this answer your question? [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – jonrsharpe Jun 12 '20 at 07:12
  • To get input as integer, you need to do this, `int(input('You text'))`. By default `input()` only gives out `string,` as said correctly by @jonrsharpe – Alok Jun 12 '20 at 07:13

2 Answers2

0

The only mistake you are making is by taking input as string,

The line number=input("Give me a number: \n") should be number=int(input("Give me a number: \n"))

If you use input() only then it takes every input as string, you need to convert that to int in your case, using int()

Prathamesh
  • 1,064
  • 1
  • 6
  • 16
0

The sort function works on both integers and strings. But if you want it to be implemented for the integers, you need to accept the input as integers for which you can use something like this.

lst=list()
i=0

while i < 3:
    number=int(input("Give me a number: \n"))
    lst.append(number)
    i=i+1
    print("\n")

lst.sort()

print("The list of ordered numbers are:")
for j in lst:
    print(j)
Stan11
  • 274
  • 3
  • 11