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.

Think back to the previous chapter. You've seen this problem before, back when you were working on the guessing game program:

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 Ml Wt v.orks v/.th ■that don-tain a dedi»>al ?o.nt-


This should be a pretty quick fix. If you use the float() function to convert the price substring, you should then be able to compare the price to 4.74:

The pvogvan, vuks wi-th ko problems "this time-

The pvogvan, vuks wi-th ko problems "this time-

The updated t°de

The updated t°de

That's much better. Your program now waits patiently until the price falls to the right level and only then does it tell the CEO that the time is right to buy a fresh batch of coffee beans.

This is great! Now I can get on with the rest of my day and I only hear when the price of beans drops to the right level. This'll save millions! I'll tell every outlet to use it worldwide.

traffic jam





Secret Service - Corp

To Whom It May Concern: Distributed Denial

■ Hnn into an apparent domain

A recent ^^^ on the from machines of service (DDoS) att c orxgxnated f world.

showed, that much o^ th s f m arou & of located in transactxons (f ^fde) resulted xn

The number of and requests wo.n a several hund:ed ,r,Us servers, res


S1<3 wers invested in thi,

In accordance ^^f^ney General we are ^

by the United States ^ ^ we take of developer of tne thing. Xn short. „lirself on notice.

, Bud Consider yourseir we,re watching you- Bud.

Yours faithfully '

Head of



That sounds weird. What happened?

Was this article helpful?

0 0

Post a comment