Using the Right Types


You've used three different types so far: strings, integers, and floating-point numbers. It's important to know not only which data types are available to you, but how to work with them. If you don't, you might end up with programs that produce unintended results.

Introducing the Trust Fund Buddy-Bad Program

The idea for the next program was to create a tool for those souls who play all day, living off a generous trust fund. The program is supposed to calculate a grand total for monthly expenditures based on user input. This grand total is meant to help those living beyond any reasonable means stay within budget so they don't ever have to think about getting a real job. But, as you may have guessed from the program's title, Trust Fund Buddy-Bad doesn't work as the programmer intended. Figure 2.9 shows a sample run.

click to expand
Figure 2.9: The monthly total should be high, but not that high. Something is wrong.

Alright, the program obviously isn't working correctly. It has a bug. But not a bug that causes it to crash, like the syntax error you saw last chapter. When a program produces unintended results but doesn't crash, it has a logical error. Based on what you already know, you might be able to figure out what's happening by looking at the code. Here's the listing:

 # Trust Fund Buddy - Bad # Demonstrates a logical error # Michael Dawson - 1/14/03 print \ """              Trust Fund Buddy Totals your monthly spending so that your trust fund doesn't run out (and you're forced to get a real job). Please enter the requested, monthly costs. Since you're rich, ignore pennies and use only dollar amounts. """ car = raw_input("Lamborghini Tune-Ups: ") rent = raw_input("Manhattan Apartment: ") jet = raw_input("Private Jet Rental: ") gifts = raw_input("Gifts: ") food = raw_input("Dining Out: ") staff = raw_input("Staff (butlers, chef, driver, assistant): ") guru = raw_input("Personal Guru and Coach: ") games = raw_input("Computer Games: ") total = car + rent + jet + gifts + food + staff + guru + games print "\nGrand Total: " + total raw_input("\n\nPress the enter key to exit.") 

It's okay if you don't see the problem right now. I'll give you one more hint, though. Take a look at the output in Figure 2.9 again. Examine the huge number that the program prints as the grand total. Then look at all the numbers the user entered. Notice any connection? Okay, whether you do or don't, read on.

Tracking Down Logical Errors

Logical errors can be the toughest bugs to fix. Since the program doesn't crash, you don't get the benefit of an error message to offer a clue. You have to observe the behavior of the program and investigate the code.

In this case, the program's output tells the story. The huge number is clearly not the sum of all the numbers the user entered. But, by looking at the numbers, you can see that the grand total printed is a concatenation of all the numbers. How did that happen? Well, if you remember, the raw_input() function returns a string. So each "number" the user enters is treated like a string. Which means that each variable in the program has a string value associated with it. So, the line

 total = car + rent + jet + gifts + food + staff + guru + games 

is not adding numbers. It's concatenating strings!

start sidebar
IN THE REAL WORLD

The + symbol works with pairs of strings as well as pairs of integers. Using the same operator for values of different types is called operator overloading. Now, "overloading" may sound like a bad thing, but actually it's a good thing. Doesn't it make sense that strings are joined using the plus sign? You immediately understand what it means. Implemented well, operator overloading can make for clearer and more elegant code.

end sidebar

Now that you know the problem, how do you fix it? Somehow those string values need to be converted to numbers. Then the program will work as intended. If only there was some way to do this. Well, as you may have guessed, there is.




Python Programming for the Absolute Beginner
Python Programming for the Absolute Beginner, 3rd Edition
ISBN: 1435455002
EAN: 2147483647
Year: 2003
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net