+ 1

Is there a way to tell python if Odd number or if even number print(this)

You have a box of popsicles and you want to give them all away to a group of brothers and sisters. If you have enough left in the box to give them each an even amount you should go for it! If not, they will fight over them, and you should eat them yourself later. My code: siblings = int(input()) popsicles = int(input()) n= siblings i= popsicles k=int(i/n) if int(i==k*n): print('give away') else: print('eat them yourself') Is there a way to say If i odd number or even number Print ()

8th Nov 2023, 8:56 PM
Kreshnik Feka
Kreshnik Feka - avatar
8 Antworten
+ 3
# Do it like this fastly num = input() is_even = True if num%2 == 0 else False # then you can just put if is_even: print(..) else: print(..)
9th Nov 2023, 6:25 PM
Harimamy Ravalohery
Harimamy Ravalohery - avatar
+ 7
Hi, Kreshnik Feka ! There is a way in Python to tell if a integer is odd or even. You use the modulo operator for that. If a nummer num is even, the following comes true: num % 2 == 0 If a nummer num is odd, the following comes true: num % 2 == 1
8th Nov 2023, 9:40 PM
Per Bratthammar
Per Bratthammar - avatar
+ 4
Kreshnik Feka , >>> besides the issues that are named by Per Bratthammar and Brian, some modifications should be done: > the code takes 2 inputs: an integer number that is assigned to the variable *siblings* an integer number that is assigned to the variable *popsicles* > then these variables get assigned to 2 new variables: n = siblings i = popsicles now the code works with the new variables *n* and *i* > these last 2 steps are not required. we don’t need these extra variables. it is recommended to use *meaningful variable names* like in the first step. using variable names like *n*, *i* and *k* is confusing and reduces the readability of the code.
9th Nov 2023, 7:48 AM
Lothar
Lothar - avatar
+ 3
The Code Coach task is poorly worded. The task is actually to determine whether the popsicles can be equally divided among the siblings, not whether the distributed amount is even or odd. Kreshnik Feka, in concept your code should work if you correct the indentation and reverse the logic. Also, you may remove the int() around the conditional to the if statement. It does not affect the result. But do correct the logic. It is printing exactly the opposite of what you want.
9th Nov 2023, 12:19 AM
Brian
Brian - avatar
+ 3
Harimamy Ravalohery , (1) the code you mentioned has one issue that causes an error. (2) suggestion to make one line of code a bit simpler. num = int(input()) # (1) input has to be an integer #is_even = True if num%2 == 0 else False is_even = num % 2 == 0 # (2) this is simpler than the current ternary print(f'{num} is even: {is_even}') # ...
10th Nov 2023, 8:31 PM
Lothar
Lothar - avatar
+ 2
siblings = int(input("Enter the number of siblings: ")) popsicles = int(input("Enter the number of popsicles: ")) if popsicles % siblings == 0: print("Give away!") else: print("Eat them yourself.")In this code, popsicles % siblings
10th Nov 2023, 5:58 PM
Atif
Atif - avatar
+ 2
Lothar thanks for your suggest Yes, you're right.. as we we know by default input() is returning always a string According to your suggest. Finally we can have just 2 lines like this num = int(input()) print(f'{num} is even: {num%2 == 0}') Nb: I didn't make the test yet
11th Nov 2023, 9:49 AM
Harimamy Ravalohery
Harimamy Ravalohery - avatar
0
Per Bratthammar, Kreshnik Feka - Modulo division exists in many general programming languages. It doesn't automatically tell if a number is odd or even though, it's just returning the remainder from a division operation. So, an odd number % 2 == 1 because 1 / 2 leaves a remainder of 1, and then of course an even number % 2 == 0.
9th Nov 2023, 8:47 PM
Tom Shaver
Tom Shaver - avatar