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
4 Answers
+ 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
+ 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
...
+ 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.
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