0

Even And Odd function #python

def evenAndOdd(y): for x in range(1,y): if (x%2==0): e+=1 elif: o+=1 ttuple =(o,e) return ttuple print(evenAndOdd(16)) it says if (x%2==0): ^ IndentationError: expected an indented block it always shows this intended block, but I don't understand what should I do

21st Oct 2018, 1:43 PM
Sara
Sara - avatar
4 ответов
+ 5
The `if` is inside the `for`, so you need to indent it more. def evenAndOdd(y): for x in range(1,y): if(x%2==0): e+=1
21st Oct 2018, 1:47 PM
Schindlabua
Schindlabua - avatar
+ 3
Yup that's true! At the beginning of your function you should probably make two variables like def evenAndOdd(y): e = 0 odd = 0 ...
21st Oct 2018, 2:08 PM
Schindlabua
Schindlabua - avatar
+ 1
Yep, this is because you’ve told your evenAndOdd function to increment your ‘odd’ variable by 1, but you’ve not told it where it should start from. You may think it’s common sense to start from 0, but computers don’t possess common sense, so you need something like this... def evenAndOdd(y): e,odd = 0,0 for x in range(1,y): if(x%2==0): e+=1 if(x%2!=0): odd+=1 return (odd,e) ... as your function. Note that you need the ‘return’ command at the end of the function or else the odds and es are counted up, but then immediately forgotten. So we would need to say... ttuple = evenAndOdd(10) ... to save the variable ‘ttuple’ as the output of the function.
21st Oct 2018, 3:25 PM
Russ
Russ - avatar
0
Thanks for your help, now it says odd is not defined;/ def evenAndOdd(y): for x in range(1,y): if(x%2==0): e+=1 if(x%2!=0): odd+=1 ttuple =(odd,e) return ttuple
21st Oct 2018, 1:57 PM
Sara
Sara - avatar