0

Popsicles

Hi all, I'm having difficulty with the code coach 'Popsicles'. I know it's easy and you have to determine whether one value divided by the other is integer or float, but I'm not sure how to determine this. If type(value) == '<class 'int'>' is producing an error. Can anyone help?

16th Jun 2024, 8:32 AM
Technocrat
9 Respostas
+ 3
Using modulo is the most direct solution, which was already described by others. If you wish not to use modulo, there is a simple alternative to determine whether a value is integer or not - regardless of variable type. Check whether the value is the same after converting it to int. if value==int(value):
16th Jun 2024, 2:06 PM
Brian
Brian - avatar
+ 6
You did not specify the programming language. Generally, to determine if a number is divisible by another, you can use the modulo operator (%)
16th Jun 2024, 8:34 AM
Tibor Santa
Tibor Santa - avatar
+ 4
popsicles % siblings This operation gives the remainder after dividing the number of popsicles by the number of siblings. If it is zero, that means you can give them away because it is divisible.
16th Jun 2024, 8:54 AM
Tibor Santa
Tibor Santa - avatar
+ 4
And your attempt was giving a syntax error because you cannot nest an apostrophe inside another apostrophe-enclosed string. Generally you should not rely on type() function, maybe very rarely. Especially in case of float division. Depending on the rest of your code, the value may always be a float or always be an int, regardless of the actuals numbers involved.
16th Jun 2024, 8:55 AM
Tibor Santa
Tibor Santa - avatar
+ 3
Sorry, I am using python.
16th Jun 2024, 8:43 AM
Technocrat
+ 3
You can check if a variable is a type of integer or float. For example: num_a = 123 print(type(num_a) == int) # True num_b = 3.14 print(type(num_b) == float) # True If you learned about class and want to check if a variable is an instance of certain class, you can use isinstance() method. For example: class my_class(): def __init__(self, name): self.name = name test_object = my_class('Technocrat') print(isinstance(test_object, my_class)) # True In general, type() and instance() are not used in a working program. However, it is useful to run them in the interpreter to see the result on the fly.
16th Jun 2024, 10:58 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 2
I have a custom function that will help with just that kind of thing. https://sololearn.com/compiler-playground/c0M66SngJj8W/?ref=app just put this code after defining the function: if type(ripi(popsicles/2))==int: print ("give away") elif type(ripi(popsicles/2))==float print("eat them yourself")
16th Jun 2024, 8:55 PM
Jonathan Dodge
Jonathan Dodge - avatar
+ 1
Nice one. Thanks. I have completed the Introduction to python course and % wasn't mentioned there, as far as I remember.
16th Jun 2024, 8:56 AM
Technocrat
0
siblings = int(input()) popsicles = int(input()) #your code goes here if popsicles % siblings == 0: print('give away') else: print('eat them yourself')
16th Jun 2024, 10:25 AM
Technocrat