# Anyone can help me?

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>
I want to make insertion sorting algorithm that user can type elements. But I have error in line 4. Anyone can help me?
<What do you expect to happen instead?>

``` List = [] a = (raw_input("Enter your each element one by one(when the last element typed, press enter):")) while a != " ": List.append([a]) if a == " ": break def insertion_sort(list): for index in range(1,len(list)): value=list[index] i=index -1 while i>=0: if value<list[i]: list[i+1]= list[i] list[i]=value i=i-1 else: break print insertion_sort(list) ```

In the `while` loop you need to use `raw_input` to get new value of `a`. And you do not have to use `break`, you already specified condition for the `while` loop:

``````List = []
a = (raw_input("Enter your each element one by one(when the last element typed, press enter):"))
while a != "":
List.append([a])
a = raw_input()
``````

You have created variable called `List`, but in the function call you use `list`, this is a major problem:

``````print insertion_sort(List)
``````

Your function is not correct, but you should be able to correct it without our help.

1 Like

you have a infinity loop. you could simply do this:

``````List = []
a = str(raw_input("Enter your each element one by one(when the last element typed, press enter):"))
while a != "":
List.append(a)
a = str(raw_input("Enter your each element one by one(when the last element typed, press enter):"))
``````

you have to re-prompt in every run of the loop, raw_input stores as uni-code, so you might want to cast to string, are you sure you want to append a list into your `List`? this is what you did? No need for the if condition, the loop condition will simply evaluate to false

1 Like

Thank you for replying!! I know it is out of my ability, but I had to do this because of my school exam… I will practice more and more about Python and try to correct code myself I feel so relieved that my if condition was almost correct!

``````List = []
a = str(raw_input("Enter your each element one by one(when the last element typed, press enter):"))
while a != "":
List.extend(a)
a = str(raw_input("Enter your each element one by one(when the last element typed, press enter):"))

def insertion_sort(List):
for index in range(1,len(List)):
value=List[index]
i=index -1
while i>=0:
if value<List[i]:
List[i+1]= List[i]
List[i]=value
i=i-1
else:
break
print insertion_sort(List)
``````

I corrected my code, but output is “None” instead of sorted list…

Your function does not `return` (that’s a hint) anything 1 Like

What should I return…?? should I return function??

your function sorts the list, so you should return the list after the sorting is complete

2 Likes

Definitely not, you made some changes to the `List` in the body of the function, right? So return the modified `List`.

Add this line at the end of the function (after the `for` loop, not inside the loop):

``````return List
``````
1 Like

List =
a = str(raw_input(“Enter your each element one by one(when the last element typed, press enter):”))
while a != “”:
List.extend(a)
a = str(raw_input(“Enter your each element one by one(when the last element typed, press enter):”))

def insertion_sort(List):
for index in range(1,len(List)):
value=List[index]
i=index -1
while i>=0:
if value<List[i]:
List[i+1]= List[i]
List[i]=value
i=i-1
else:
break
return List
print insertion_sort(List)

This code works strangely… I put 1,34,12, output should be [1,12,34]but It print [‘1’,‘1’,‘2’,‘3’,‘4’]…I’m sorry for making mistakes…

ah, you want to have lists in the numbers. in that case, i recommend casting the input to integers using `int()` rather then `str()`

you must realize, that `raw_input` has no clue of the data type you enter, so it stores as unicode by default, if you need a different data type, you nneed to do casting

1 Like

But it makes error if I type 2 elements…

Use `append`, just like in the previous version of your code (why you changed this?). Cast `a` to int only when you are sure that it’s not an empty string.

``````List = []
a = raw_input("Enter your each element one by one(when the last element typed, press enter):")
while a != "":
List.append(int(a))
a = raw_input("Enter your each element one by one(when the last element typed, press enter):")
``````

You should now add an `if` construction to check if `a` is really an integer.

1 Like

How kind of you!! I’m so appreciate your explanation and coding ability! i’m so happy that I wouldn’t stay up all night because of this and I will get bonus point! Thank you:joy:

You’re welcome This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.