Naomi Jacobson
Naomi Jacobson

Reputation: 107

int() vs float() inputs in Python 3 and their utility?

This doesn't refer to a specific code of mine, so I hope this does not defy community standards for asking questions. I'm still learning, so please let me know if this kind of question is inappropriate for future reference!

I am trying to gain a thorough understanding of the utility of certain commands as I embark on learning to use Python 3. I have never coded before, so I do not have background in any other language. I was hoping someone could help me understand this more thoroughly.

Basically, I understand that when prompting for a user input with a numeric value, it is sometimes correct to write float(input()), and sometimes correct to write int(input()). I know in mathematics that an integer is a whole number, and a floating point number is any number defined with a whole portion, a radix, and a mantissa (like 4.32). I don't understand the utility of converting a user input to one or the other.

For example, if I write int(input("Input a decimal. ")) and the user inputs 4.3, the program will return a value error. What is the utility in this? So:

  1. What is the utility in converting an input() to float() or int()?
  2. I understand when I would want an integer (e.g; if I want the user to input how many times to multiply a particular number by itself), but why would I want a floating point input?
  3. In general, when do I need to implement either, and how can I recognize which one a program needs?
  4. Aside from user input, in what other general cases would I want to implement either command?
  5. If anyone has any additional reading on how and when to convert certain defined variables or inputs, please send them my way!

EDIT:

Here is an example of a code that I wrote that I think highlights my confusion about if and when to use int() and float():

price=input("How much did the item cost?: $")
if float(price)<0:
    print("The price cannot be negative.")
else:
    price=int(float(price)*100)
    paid=input("How much did the customer pay?: $")
    paid=int(float(paid)*100)

Did I do this correctly? The larger program of which this is a part works fine, but I'm not sure if I added unnecessary command or implemented the commands correctly.

Thank you so much for your help!

Naomi

Upvotes: 4

Views: 24024

Answers (4)

HandyGold75
HandyGold75

Reputation: 53

In my opinion, you should always use float whenever you're not sure because it accepts more values so it will less likely return an error.

Unless your code expects a rounded number (you can round the number if it's a float but can cause confusion if done incorrectly) from a users input.

Bad example:

username_input = float(input("Enter your username: "))
pincode_input = int(input("Enter pincode: "))

pincode = 1234

if pincode == pincode_input:
    print("Good")
else:
    print("Bad")

In this case, the code expects the user to input a rounded number so I like to use int() so that if the user inputs a decimal number then I know where the problem is and can fix it directly instead of fixing every other code that relies on it.

This is a bad example because the code does run if the user's input doesn't get converted with int() but I hope you get the idea.

Upvotes: 0

nsy16
nsy16

Reputation: 1

You can implement error checking using the try function. The only reason to use an int vs a float, would be if you are trying to save memory, in the case of embedded ROM where you are severly limited.

Upvotes: 0

dursk
dursk

Reputation: 4445

It has nothing about utility, it has to do with what are the possible range of values you're program should/needs to accept.

If it needs to accept both integers and floats as inputs, then you should convert to float since floats can represent the integers.

But if you're program requires that the input be specifically an integer, then you should be casting to int.

EDIT:

In your example, you should always be using float, since money has a decimal value.

If you were asking "How many bananas did you buy?" You'd want to convert to int since those values are going to be 0, 1, 2, 3, 4, .... And then when you ask "How much did you pay for these bananas?" You'd want to convert to float since those inputs can range from 3.15, .77, 1, 1.00, ... etc.

Upvotes: 6

TigerhawkT3
TigerhawkT3

Reputation: 49318

  1. So that you can work with numbers. You can't very well multiply '3' by '2' in Python.
  2. So that you can work with floating-point numbers. Some things in life can come in bits and pieces, like kilograms, seconds, or grade point averages.
  3. If your program needs to work with the numbers between consecutive integers, you should probably use float.
  4. If you use strings in your program, you may want them to be numbers, regardless of where the strings came from.
  5. You'll need to evaluate this on a case-by-case basis.

Upvotes: 0

Related Questions