Why is total_cost a float?



If cucumbers isn’t a float, how did total_cost end up being a float?


When we do math between an integer and a float, the result will always be a float. This is a good rule to keep in mind because it can cause unexpected and incorrect values if you try dividing two integer values that are not evenly divisible. The simple solution is what we’ve done in this exercise: convert one of the involved numbers to a float so that the result becomes a float.

Take a look at these examples:

my_float = 70.
float_2  = 70.0
my_int   = 70

float_result   = 70 / 2.0
float_result_2 = 70. / 2.0
float_result_3 = 70.0 / 2

int_result = 70 / 7

Try running that code and printing the values. Were the results as you expected?


At Numbers, section 9/14 in the first part of learning Python, I was fine with instructions 1-3 but got stopped on #4, "Print out total_cost. What datatype is it?"
Usually there is a hint available to concisely explain what to do, but not in this case.
What is wrong with my code? and where did I miss the instruction?

1 cucumbers = 1
2 price_per_cucumber = 3.25
3 total_cost = cucumbers * price_per_cucumber
4 print “total_cost”


Please include a link to the exercise that this relates to. The example given has no context.

Python 2 division quotient is dependent on data types of the operands. If both are integer, the quotient is integer. When one or the other, or both are floats, the quotient is a float.

7 / 3  =>  2

7 / 3. => 2.3333333333333335

From a standpoint of explicitness in readability,

float(7) / 3  => 2.3333333333333335

Python 3 has removed this conditional treatment of quotients by making all quotients a float, by default.

5 / 2  => 2.5

If an integer is the intended result, then explicit casting is required…

int(5 / 2)  =>  2

If the intention is to print the value referred to by the variable, total_cost then leave off the quotes. The above is outputting…


rather than the expected,

>>> cucumbers = 1
>>> price_per_cucumber = 3.25
>>> total_cost = cucumbers * price_per_cucumber
>>> print total_cost
>>> type(total_cost)
<type 'float'>

There it is. I was unnecessarily employing quotes. Thank you.

1 Like

A post was split to a new topic: Multi-Line strings

syntax error on <type ‘float’>


<type ‘float’> is the output of type(total_cost), <type ‘float’> shouldn’t be ran as code

1 Like

At section 9/14 for Python Syntax, I’m stuck as to why #4 isn’t shown as completed.

cucumbers = 93
print "Number of cucumbers: ", cucumbers

price_per_cucumber = 3.25
print "Price per cucumber: ", price_per_cucumber

total_cost = cucumbers * price_per_cucumber
print "Total: ", total_cost

print type(total_cost)

That line is unexpected, probably, and should be removed.

Whenever you see code written like this,

>>> type(total_cost)
<type 'float'>

it means the command was entered directly at the interactive console, and the interpreter responds immediately. In a code statement we need to assign expressions such as this, or print them.

That should output what you expect.

1 Like

A post was split to a new topic: Use print to print total_cost

A post was split to a new topic: Shopping trip

Why should I write “print” before “total_cost”?

The only reason would be so we can see the outcome displayed on the screen.