Search code examples
pythonwindowslistpython-idle

Can´t use a list with more than one decimal place as input in python


I created an algorithm that organizes a list using sequential search and I am having a problem. I'm using Idle on Windows to program in python (version 3.4.2 ) and this happens:

https://i.sstatic.net/iohvl.png

As you can see(I hope) i tried to create an input with numbers with more than one decimal place, and it worked in this strange way. My question is: how can i fix it?

Here is the code:

#coding:utf8
import time
x=list(input("Type a list:"))
start=time.time()
lista=[]
for a in range(len(x)):
    for b in range(len(x)-1):
    for c in range(b+1,b+2):
        if x[b]>x[c]:
            lista.append(x[b])
            lista.append(x[c])
            x[b]=lista[1]
            x[c]=lista[0]
            lista=[]
        print (x)
        end=time.time()
        print ((end-start)*1000,"ms") 

If you cant see the image:

Input:['1','23','45','67','89','43','51','23','34']

Output:["'", "'", "'", "'", "'", "'", "'", "'", "'", "'", "'", "'", "'", "'", "'", "'", "'", "'", ',', ',', ',', ',', ',', ',', ',', ',', '1', '1', '2', '2', '3', '3', '3', '3', '4', '4', '4', '5', '5', '6', '7', '8', '9', '[', ']']

Solution

  • This line:

    x=list(input("Type a list:"))
    

    does nothing that helps you. You need to parse the string.

    >>> ast.literal_eval('[1,2,3]')
    [1, 2, 3]