+ 2
whats wrong on my code here?
def is_even(x): if x%2==0: return True else: return False def main(): x=int(input("whats your x?")) if is_even(x): print("even") else: print("odd") main()
5 Respostas
+ 4
The first line of code have a unexpected indent:
` def is_even(x):`
∆
[]
Remove this space then it'll work!
Remember that Python doesn't work like C++, C# or Java, where you can place your indent at any place you want!
When you don't have to execute something inside a statement that have a body(In python, this means that the code inside it must be indented and each line should be indented the same as the other), you don't need indentation!
Example:
def hello():. #This line doesn't need indentation
print("Hi") #This line needs indentation, since you want it to be in the hello() function
hello() #This line doesn't need indentation, because it wasn't in a statement with a body.
+ 1
yes of course I also use functions and they are very convenient for making the code more elegant and not repeating pieces of code. I was just saying to arrange the two functions with indentation and maybe only in one for simplicity but obviously everyone writes the code as he wants. I didn't want to judge but only to advise and in any case pardon x it is an argument.
0
You could transform in a only function and 'x' as argument of first function is not correct because there isn't an argument of x (variable of x in 'def is_even(x)').
You should add indentation and you should put 'x=int(input())' before in the first def or out of the def.
Transform in a only def with def is_even() and write only one time if x%2==0 ecc...
0
Glo What do you mean bro, "x"in the code above is an argument(parameter) passed to the function, so it can only access to the function itself. You can declare a variable with the same name as the given parameter (when defining a function) of a function.
Don't judge the use of functions, because it helps us to reduce code line, and making it understandable for the one who is reading the code!(As if the programmer put a meaningful name to it).
These simple example helps the learner to get started with functions..