Python function on if/return [closed]









up vote
-3
down vote

favorite












I am doing code academy and came across this question:




Create a function called middle_element that has one parameter named
lst.



If there are an odd number of elements in lst, the function should
return the middle element. If there are an even number of elements,
the function should return the average of the middle two elements.




This was my code:



def middle_element(lst):
if (len(lst)%2) = 0:
return lst[len(lst)/2]
else:
firstel= lst[len(lst/2-1)]
secel= lst[len(lst/2+1)]
avg = firstel+secel/2
return avg


Unable to compile due to Syntax error



:( Can anyone advise me? Thank you!










share|improve this question















closed as off-topic by roganjosh, snakecharmerb, jpp, lucascaro, Devon_C_Miller Nov 12 at 5:20


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – roganjosh, snakecharmerb, jpp, lucascaro, Devon_C_Miller
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 3




    Yes, your indentation is incorrect. The else is outside of the function body currently
    – roganjosh
    Nov 11 at 10:23










  • Also if needs to compare, so two == signs, then the code "return lst[len(lst)/2]" you want to execute when the list contains an odd number of elements, so you need to check for "== 1" not "== 0". And the expression "len(lst/2+1)" is also wrong. The +1 must be outside of the len-function call
    – quant
    Nov 11 at 10:44






  • 1




    oh my. Thanks so much for your help!
    – Hong Wei
    Nov 11 at 10:54














up vote
-3
down vote

favorite












I am doing code academy and came across this question:




Create a function called middle_element that has one parameter named
lst.



If there are an odd number of elements in lst, the function should
return the middle element. If there are an even number of elements,
the function should return the average of the middle two elements.




This was my code:



def middle_element(lst):
if (len(lst)%2) = 0:
return lst[len(lst)/2]
else:
firstel= lst[len(lst/2-1)]
secel= lst[len(lst/2+1)]
avg = firstel+secel/2
return avg


Unable to compile due to Syntax error



:( Can anyone advise me? Thank you!










share|improve this question















closed as off-topic by roganjosh, snakecharmerb, jpp, lucascaro, Devon_C_Miller Nov 12 at 5:20


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – roganjosh, snakecharmerb, jpp, lucascaro, Devon_C_Miller
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 3




    Yes, your indentation is incorrect. The else is outside of the function body currently
    – roganjosh
    Nov 11 at 10:23










  • Also if needs to compare, so two == signs, then the code "return lst[len(lst)/2]" you want to execute when the list contains an odd number of elements, so you need to check for "== 1" not "== 0". And the expression "len(lst/2+1)" is also wrong. The +1 must be outside of the len-function call
    – quant
    Nov 11 at 10:44






  • 1




    oh my. Thanks so much for your help!
    – Hong Wei
    Nov 11 at 10:54












up vote
-3
down vote

favorite









up vote
-3
down vote

favorite











I am doing code academy and came across this question:




Create a function called middle_element that has one parameter named
lst.



If there are an odd number of elements in lst, the function should
return the middle element. If there are an even number of elements,
the function should return the average of the middle two elements.




This was my code:



def middle_element(lst):
if (len(lst)%2) = 0:
return lst[len(lst)/2]
else:
firstel= lst[len(lst/2-1)]
secel= lst[len(lst/2+1)]
avg = firstel+secel/2
return avg


Unable to compile due to Syntax error



:( Can anyone advise me? Thank you!










share|improve this question















I am doing code academy and came across this question:




Create a function called middle_element that has one parameter named
lst.



If there are an odd number of elements in lst, the function should
return the middle element. If there are an even number of elements,
the function should return the average of the middle two elements.




This was my code:



def middle_element(lst):
if (len(lst)%2) = 0:
return lst[len(lst)/2]
else:
firstel= lst[len(lst/2-1)]
secel= lst[len(lst/2+1)]
avg = firstel+secel/2
return avg


Unable to compile due to Syntax error



:( Can anyone advise me? Thank you!







python function






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 13:17









Theo

2,8261518




2,8261518










asked Nov 11 at 10:16









Hong Wei

1




1




closed as off-topic by roganjosh, snakecharmerb, jpp, lucascaro, Devon_C_Miller Nov 12 at 5:20


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – roganjosh, snakecharmerb, jpp, lucascaro, Devon_C_Miller
If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by roganjosh, snakecharmerb, jpp, lucascaro, Devon_C_Miller Nov 12 at 5:20


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – roganjosh, snakecharmerb, jpp, lucascaro, Devon_C_Miller
If this question can be reworded to fit the rules in the help center, please edit the question.







  • 3




    Yes, your indentation is incorrect. The else is outside of the function body currently
    – roganjosh
    Nov 11 at 10:23










  • Also if needs to compare, so two == signs, then the code "return lst[len(lst)/2]" you want to execute when the list contains an odd number of elements, so you need to check for "== 1" not "== 0". And the expression "len(lst/2+1)" is also wrong. The +1 must be outside of the len-function call
    – quant
    Nov 11 at 10:44






  • 1




    oh my. Thanks so much for your help!
    – Hong Wei
    Nov 11 at 10:54












  • 3




    Yes, your indentation is incorrect. The else is outside of the function body currently
    – roganjosh
    Nov 11 at 10:23










  • Also if needs to compare, so two == signs, then the code "return lst[len(lst)/2]" you want to execute when the list contains an odd number of elements, so you need to check for "== 1" not "== 0". And the expression "len(lst/2+1)" is also wrong. The +1 must be outside of the len-function call
    – quant
    Nov 11 at 10:44






  • 1




    oh my. Thanks so much for your help!
    – Hong Wei
    Nov 11 at 10:54







3




3




Yes, your indentation is incorrect. The else is outside of the function body currently
– roganjosh
Nov 11 at 10:23




Yes, your indentation is incorrect. The else is outside of the function body currently
– roganjosh
Nov 11 at 10:23












Also if needs to compare, so two == signs, then the code "return lst[len(lst)/2]" you want to execute when the list contains an odd number of elements, so you need to check for "== 1" not "== 0". And the expression "len(lst/2+1)" is also wrong. The +1 must be outside of the len-function call
– quant
Nov 11 at 10:44




Also if needs to compare, so two == signs, then the code "return lst[len(lst)/2]" you want to execute when the list contains an odd number of elements, so you need to check for "== 1" not "== 0". And the expression "len(lst/2+1)" is also wrong. The +1 must be outside of the len-function call
– quant
Nov 11 at 10:44




1




1




oh my. Thanks so much for your help!
– Hong Wei
Nov 11 at 10:54




oh my. Thanks so much for your help!
– Hong Wei
Nov 11 at 10:54












1 Answer
1






active

oldest

votes

















up vote
1
down vote













Sigh, probably a total beginner. Let's fix the indents first, so your code is looking like this.



def middle_element(lst):
if (len(lst)%2) = 0:
return lst[len(lst)/2]
else:
firstel= lst[len(lst/2-1)]
secel= lst[len(lst/2+1)]
avg = firstel+secel/2
return avg


Next to fix is the = sign in the comparison. You don't want to assign a variable here, but compare, so you need



def middle_element(lst):
if (len(lst)%2) == 0:
return lst[len(lst)/2]
else:
firstel= lst[len(lst/2-1)]
secel= lst[len(lst/2+1)]
avg = firstel+secel/2
return avg


However now you are executing the statement return lst[len(lst)/2] when the list has an even number of elements. You want to have this the other way round. This statement should get executed when there are an odd number of elements in the list, so:



def middle_element(lst):
if (len(lst)%2) == 1:
return lst[len(lst)/2]
else:
firstel= lst[len(lst/2-1)]
secel= lst[len(lst/2+1)]
avg = firstel+secel/2
return avg


As a minor change I would transform



return lst[len(lst)/2]


into



return lst[len(lst)//2]


to prevent problems with python3. Now to the coder in the else-part which is totally wrong too, it should be



firstel= lst[len(lst)//2]
secel= lst[len(lst)//2+1]
avg = (firstel+secel)/2
return avg


Since the len()-function expects a list, not a list divided by something. Furthermore you need braces between (firstel+secel)/2 otherwise you would calculate firstel+(secel/2) which is not the average. So in total:



from __future__ import division

def middle_element(lst):
if len(lst) % 2 == 1:
return lst[len(lst) // 2]
else:
firstel = lst[len(lst) // 2 - 1]
secel = lst[len(lst) // 2]
avg = (firstel + secel) / 2
return avg





share|improve this answer




















  • Thank you for your detailed explanation! And yes I'm new to coding so do bear with newbies like me. Thank you so much!
    – Hong Wei
    Nov 11 at 11:24






  • 1




    @HongWei On stackoverflow when you like a question / an answer you are upwoting / accepting it. Don't comment it, see meta.stackexchange.com/questions/173399/…
    – quant
    Nov 11 at 11:26

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













Sigh, probably a total beginner. Let's fix the indents first, so your code is looking like this.



def middle_element(lst):
if (len(lst)%2) = 0:
return lst[len(lst)/2]
else:
firstel= lst[len(lst/2-1)]
secel= lst[len(lst/2+1)]
avg = firstel+secel/2
return avg


Next to fix is the = sign in the comparison. You don't want to assign a variable here, but compare, so you need



def middle_element(lst):
if (len(lst)%2) == 0:
return lst[len(lst)/2]
else:
firstel= lst[len(lst/2-1)]
secel= lst[len(lst/2+1)]
avg = firstel+secel/2
return avg


However now you are executing the statement return lst[len(lst)/2] when the list has an even number of elements. You want to have this the other way round. This statement should get executed when there are an odd number of elements in the list, so:



def middle_element(lst):
if (len(lst)%2) == 1:
return lst[len(lst)/2]
else:
firstel= lst[len(lst/2-1)]
secel= lst[len(lst/2+1)]
avg = firstel+secel/2
return avg


As a minor change I would transform



return lst[len(lst)/2]


into



return lst[len(lst)//2]


to prevent problems with python3. Now to the coder in the else-part which is totally wrong too, it should be



firstel= lst[len(lst)//2]
secel= lst[len(lst)//2+1]
avg = (firstel+secel)/2
return avg


Since the len()-function expects a list, not a list divided by something. Furthermore you need braces between (firstel+secel)/2 otherwise you would calculate firstel+(secel/2) which is not the average. So in total:



from __future__ import division

def middle_element(lst):
if len(lst) % 2 == 1:
return lst[len(lst) // 2]
else:
firstel = lst[len(lst) // 2 - 1]
secel = lst[len(lst) // 2]
avg = (firstel + secel) / 2
return avg





share|improve this answer




















  • Thank you for your detailed explanation! And yes I'm new to coding so do bear with newbies like me. Thank you so much!
    – Hong Wei
    Nov 11 at 11:24






  • 1




    @HongWei On stackoverflow when you like a question / an answer you are upwoting / accepting it. Don't comment it, see meta.stackexchange.com/questions/173399/…
    – quant
    Nov 11 at 11:26














up vote
1
down vote













Sigh, probably a total beginner. Let's fix the indents first, so your code is looking like this.



def middle_element(lst):
if (len(lst)%2) = 0:
return lst[len(lst)/2]
else:
firstel= lst[len(lst/2-1)]
secel= lst[len(lst/2+1)]
avg = firstel+secel/2
return avg


Next to fix is the = sign in the comparison. You don't want to assign a variable here, but compare, so you need



def middle_element(lst):
if (len(lst)%2) == 0:
return lst[len(lst)/2]
else:
firstel= lst[len(lst/2-1)]
secel= lst[len(lst/2+1)]
avg = firstel+secel/2
return avg


However now you are executing the statement return lst[len(lst)/2] when the list has an even number of elements. You want to have this the other way round. This statement should get executed when there are an odd number of elements in the list, so:



def middle_element(lst):
if (len(lst)%2) == 1:
return lst[len(lst)/2]
else:
firstel= lst[len(lst/2-1)]
secel= lst[len(lst/2+1)]
avg = firstel+secel/2
return avg


As a minor change I would transform



return lst[len(lst)/2]


into



return lst[len(lst)//2]


to prevent problems with python3. Now to the coder in the else-part which is totally wrong too, it should be



firstel= lst[len(lst)//2]
secel= lst[len(lst)//2+1]
avg = (firstel+secel)/2
return avg


Since the len()-function expects a list, not a list divided by something. Furthermore you need braces between (firstel+secel)/2 otherwise you would calculate firstel+(secel/2) which is not the average. So in total:



from __future__ import division

def middle_element(lst):
if len(lst) % 2 == 1:
return lst[len(lst) // 2]
else:
firstel = lst[len(lst) // 2 - 1]
secel = lst[len(lst) // 2]
avg = (firstel + secel) / 2
return avg





share|improve this answer




















  • Thank you for your detailed explanation! And yes I'm new to coding so do bear with newbies like me. Thank you so much!
    – Hong Wei
    Nov 11 at 11:24






  • 1




    @HongWei On stackoverflow when you like a question / an answer you are upwoting / accepting it. Don't comment it, see meta.stackexchange.com/questions/173399/…
    – quant
    Nov 11 at 11:26












up vote
1
down vote










up vote
1
down vote









Sigh, probably a total beginner. Let's fix the indents first, so your code is looking like this.



def middle_element(lst):
if (len(lst)%2) = 0:
return lst[len(lst)/2]
else:
firstel= lst[len(lst/2-1)]
secel= lst[len(lst/2+1)]
avg = firstel+secel/2
return avg


Next to fix is the = sign in the comparison. You don't want to assign a variable here, but compare, so you need



def middle_element(lst):
if (len(lst)%2) == 0:
return lst[len(lst)/2]
else:
firstel= lst[len(lst/2-1)]
secel= lst[len(lst/2+1)]
avg = firstel+secel/2
return avg


However now you are executing the statement return lst[len(lst)/2] when the list has an even number of elements. You want to have this the other way round. This statement should get executed when there are an odd number of elements in the list, so:



def middle_element(lst):
if (len(lst)%2) == 1:
return lst[len(lst)/2]
else:
firstel= lst[len(lst/2-1)]
secel= lst[len(lst/2+1)]
avg = firstel+secel/2
return avg


As a minor change I would transform



return lst[len(lst)/2]


into



return lst[len(lst)//2]


to prevent problems with python3. Now to the coder in the else-part which is totally wrong too, it should be



firstel= lst[len(lst)//2]
secel= lst[len(lst)//2+1]
avg = (firstel+secel)/2
return avg


Since the len()-function expects a list, not a list divided by something. Furthermore you need braces between (firstel+secel)/2 otherwise you would calculate firstel+(secel/2) which is not the average. So in total:



from __future__ import division

def middle_element(lst):
if len(lst) % 2 == 1:
return lst[len(lst) // 2]
else:
firstel = lst[len(lst) // 2 - 1]
secel = lst[len(lst) // 2]
avg = (firstel + secel) / 2
return avg





share|improve this answer












Sigh, probably a total beginner. Let's fix the indents first, so your code is looking like this.



def middle_element(lst):
if (len(lst)%2) = 0:
return lst[len(lst)/2]
else:
firstel= lst[len(lst/2-1)]
secel= lst[len(lst/2+1)]
avg = firstel+secel/2
return avg


Next to fix is the = sign in the comparison. You don't want to assign a variable here, but compare, so you need



def middle_element(lst):
if (len(lst)%2) == 0:
return lst[len(lst)/2]
else:
firstel= lst[len(lst/2-1)]
secel= lst[len(lst/2+1)]
avg = firstel+secel/2
return avg


However now you are executing the statement return lst[len(lst)/2] when the list has an even number of elements. You want to have this the other way round. This statement should get executed when there are an odd number of elements in the list, so:



def middle_element(lst):
if (len(lst)%2) == 1:
return lst[len(lst)/2]
else:
firstel= lst[len(lst/2-1)]
secel= lst[len(lst/2+1)]
avg = firstel+secel/2
return avg


As a minor change I would transform



return lst[len(lst)/2]


into



return lst[len(lst)//2]


to prevent problems with python3. Now to the coder in the else-part which is totally wrong too, it should be



firstel= lst[len(lst)//2]
secel= lst[len(lst)//2+1]
avg = (firstel+secel)/2
return avg


Since the len()-function expects a list, not a list divided by something. Furthermore you need braces between (firstel+secel)/2 otherwise you would calculate firstel+(secel/2) which is not the average. So in total:



from __future__ import division

def middle_element(lst):
if len(lst) % 2 == 1:
return lst[len(lst) // 2]
else:
firstel = lst[len(lst) // 2 - 1]
secel = lst[len(lst) // 2]
avg = (firstel + secel) / 2
return avg






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 10:56









quant

1,55911526




1,55911526











  • Thank you for your detailed explanation! And yes I'm new to coding so do bear with newbies like me. Thank you so much!
    – Hong Wei
    Nov 11 at 11:24






  • 1




    @HongWei On stackoverflow when you like a question / an answer you are upwoting / accepting it. Don't comment it, see meta.stackexchange.com/questions/173399/…
    – quant
    Nov 11 at 11:26
















  • Thank you for your detailed explanation! And yes I'm new to coding so do bear with newbies like me. Thank you so much!
    – Hong Wei
    Nov 11 at 11:24






  • 1




    @HongWei On stackoverflow when you like a question / an answer you are upwoting / accepting it. Don't comment it, see meta.stackexchange.com/questions/173399/…
    – quant
    Nov 11 at 11:26















Thank you for your detailed explanation! And yes I'm new to coding so do bear with newbies like me. Thank you so much!
– Hong Wei
Nov 11 at 11:24




Thank you for your detailed explanation! And yes I'm new to coding so do bear with newbies like me. Thank you so much!
– Hong Wei
Nov 11 at 11:24




1




1




@HongWei On stackoverflow when you like a question / an answer you are upwoting / accepting it. Don't comment it, see meta.stackexchange.com/questions/173399/…
– quant
Nov 11 at 11:26




@HongWei On stackoverflow when you like a question / an answer you are upwoting / accepting it. Don't comment it, see meta.stackexchange.com/questions/173399/…
– quant
Nov 11 at 11:26



這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3