Even And Odd function #python | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
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 Respostas
+ 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