Page 99 -
P. 99
type differences
Strings and numbers are different
The program crashed because it tried to compare a string with
a number, which is something that doesn’t make a lot of sense
to a computer program. When a piece of data is classified as a
string or a number, this refers to more than just the contents of the
variable. We are also referring to its datatype. If two pieces of
data are different types, we can’t compare them to each other.
You‛re just
“A” 1
not my type.
Think back to the previous chapter. You’ve seen this problem before,
back when you were working on the guessing game program:
This variable will be “g" is a string.
set to a number.
string into an integer, which is then
guess = int(g) The int() function converts the “g"
assigned to “guess".
In the guessing-game program, you needed to convert the user’s guess
into an integer (a whole number) by using the int() function.
But coffee bean prices aren’t whole numbers, because they contain
numbers after a decimal point. They are floating point numbers
or floats, and to convert a string to a float, you need to use a
function other than int(). You need to use float():
Like int(), but works with numbers
float("4.99")
that contain a decimal point.
64 Chapter 2