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 ответов
+ 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