+ 1

The code will run successful, but in the output at the end, you'll see None, why?

# creating a function to determine the evens or odds. def evens_or_odds(num): if num == 0: print(f'{num} is neither even or odd') elif num % 2 == 0: print(str(num) + ' is even number') else: print(num,'is an odd number',sep = " ") result = evens_or_odds(int(input('Enter a number: '))) print(result)

8th Sep 2024, 1:54 PM
Aaron Paul
Aaron Paul - avatar
8 Respuestas
+ 5
Aaron Paul Just remove the last line print(result) You don't need it.
8th Sep 2024, 2:24 PM
BroFar
BroFar - avatar
+ 4
Aaron Paul , None is printed, because the function `evens_or_odds()` does *not explicitly return* a value. in this case `None` is returned. you can rework the function so that no print is done there, but the output message is returned to the caller and then printed: def evens_or_odds(num): if num == 0: return f'{num} is neither even or odd' elif num % 2 == 0: return f'{num} is even number' else: return f'{num} is an odd number' result = evens_or_odds(int(input('Enter a number: '))) print(result)
8th Sep 2024, 6:28 PM
Lothar
Lothar - avatar
+ 2
evens_or_odds is a void function. it returns None. That's why printing the return value prints None. You don't have to assign the return value of the function to a variable to print it. The function prints the result for you. Just do: evens_or_odds(int(input('Enter a number: ')) one liner if you' want hard to read, not-recommended coding just to trigger the code style nazis 😅: print(n:=int(input('Enter a number:\n')), ('is neither even or odd' if n==0 else 'is even number' if n%2==0 else 'is odd number'))
9th Sep 2024, 2:32 AM
Bob_Li
Bob_Li - avatar
+ 1
hayriii zero is neither as it is not divisible by 2 nor is it a property of ...
9th Sep 2024, 3:53 PM
BroFar
BroFar - avatar
+ 1
hayriii while true to modern mathematics 0/2 == 0 and according to normal academics it is considered true the reality is false positive and the Chinese whom actually came to understand the null value of zero was first to recognize that it was neither odd nor even. |0| It is referred mostly as a placeholder of the absence of all value.
10th Sep 2024, 1:14 AM
BroFar
BroFar - avatar