MrKat MrKat - 11 months ago 49
Python Question

How can test a variable if it is an integer?

As the title might specify, I want to ask how can I test a variable if it is an integer. I have seen other topics and they don't seem to have code that works or that I know how to use.

print("Enter the weight of the bag in grams.")

weight = float(input()) # float was used as it might be a decimal sometimes

amtcoin = weight / 3.56

How do I test if amtcoin is a integer? As the variable suggests, it is the amount of coins and you cannot have a fraction of a coin.

Answer Source

amtcoin variable is instance of class you declare it. Declare it as float and if you want to know if it's a fraction or not check amtcoin % 1 == 0