0

The popsicle question

I don't get it My python code is wrong but I don't know why. Can someone help me? siblings = int(input()) popsicles = int(input()) #your code goes here x = siblings % popsicles if x == 0: ('give away') else: ('eat them yourself') UPDATED CODE: Update the problem was the indentation. I did not put it in any way I wrote the code This is the code at the moment: x = siblings % popsicles if x == 0: print ('give away') else: print ('eat them yourself') It works for test case #1, 4 and 5 But not for test case #2 and 3. It says that my output is 'eat them all' but it should be 'give away'. I get the impression that this code should get the right output or I'm missing something?

21st Mar 2024, 12:15 PM
Jordan
6 Antworten
+ 4
Jordan Suppose you have 3 siblings and 6 popsicles. You would give them away because everyone can have 2. How would you calculate that? You would check whether the number of popsicles is divisible by the number of siblings with no remainder. Now look at your calculation again ;) And as Wong Hei Ming mentioned you need to print your output. Btw: instead of calculating x and then check if x == 0 you can do it in one line. Just replace x by your calculation. if _ % _ == 0: print(...) _ is just a placeholder
21st Mar 2024, 4:09 PM
Denise Roßberg
Denise Roßberg - avatar
+ 4
this code is almost good, written is python.... don't forget, in python: the white space matters!
21st Mar 2024, 5:25 PM
Mihaly Nyilas
Mihaly Nyilas - avatar
+ 2
It is not printing anything... and a logical error...
21st Mar 2024, 1:13 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 2
Jordan let's look at case 2: 10 20 output: give away You have 10 siblings and 20 popcicles. 20 is divisible by 10, means every sibling can get 2 popcicles -> give away But in your code you check if 10 is divisible by 20, which is not 0. That's why you get the wrong output.
22nd Mar 2024, 2:25 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
I found out my mistake (I feel dumb 🙃) LAST UPDATE I saw some codes that was like 15 lines of codes but I knew there was an easier and cleaner way to keep the code as simple but valid with every input that would be given. siblings = int(input()) popsicles = int(input()) #your code goes here if popsicles % siblings == 0: print ('give away') else: print ('eat them yourself') So I just switched in the "if statement" the position of popsicles and siblings. Now the code works for every test case.
25th Mar 2024, 7:41 PM
Jordan
0
Update the problem was the indentation. I did not put it in any way I wrote the code This is the code at the moment: x = siblings % popsicles if x == 0: print ('give away') else: print ('eat them yourself') It works for test case #1, 4 and 5 But not for test case #2 and 3. It says that my output is 'eat them all' but it should be 'give away'. I get the impression that this code should get the right output or I'm missing something?
22nd Mar 2024, 11:34 AM
Jordan