# Converting Values

## Converting Values

The solution to the Trust Fund Buddy-Bad program is to convert the string values returned by raw_input() to numeric ones. Since the program works with whole dollar amounts, it makes sense to convert each string to an integer before working with it.

### Introducing the Trust Fund Buddy-Good Program

The Trust Fund Buddy-Good program fixes the logical bug in Trust Fund Buddy-Bad. Take a look at the output of the new program in Figure 2.10.

Now the program arrives at the correct total. Here's the code:

```# Trust Fund Buddy - Good # Demonstrates type conversion # 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: ") car = int(car) rent = int(raw_input("Manhattan Apartment: ")) jet = int(raw_input("Private Jet Rental: ")) gifts = int(raw_input("Gifts: ")) food = int(raw_input("Dining Out: ")) staff = int(raw_input("Staff (butlers, chef, driver, assistant): ")) guru = int(raw_input("Personal Guru and Coach: ")) games = int(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.")
```

### Converting Strings to Integers

There are several functions that convert between types. The function to convert a value to an integer is demonstrated in the following lines:

```car = raw_input("Lamborghini Tune-Ups: ") car = int(car)
```

The first line is just like before. It gets input from the user as a string and assigns that value to car . The second line does the conversion. The function int() takes the string referenced by car and converts it to an integer. Then, car gets this new integer value.

The next seven lines get and convert the remaining expenditure categories:

```rent = int(raw_input("Manhattan Apartment: ")) jet = int(raw_input("Private Jet Rental: ")) gifts = int(raw_input("Gifts: ")) food = int(raw_input("Dining Out: ")) staff = int(raw_input("Staff (butlers, chef, driver, assistant): ")) guru = int(raw_input("Personal Guru and Coach: ")) games = int(raw_input("Computer Games: "))
```

Notice that the assignments are done in just one line now. That's because the two function calls, raw_input() and int() , are nested. Nesting function calls means putting one inside the other. This is perfectly fine as long as the return values of the inner function can be used by the outer function. Here, the return value of raw_input() is a string, and a string is a perfectly acceptable type for int() to convert.

In the assignment statement for rent , raw_input() goes out and asks the user how much the rent was. The user enters some text, and that is returned as a string. Then, the program calls the function int() with that string. int() returns the integer the string represented. Then, that integer is assigned to rent . The other six assignment statements work the same way.

There are other functions that convert values to a specific type. Table 2.5 lists several.

Table 2.5: SELECTED TYPE CONVERSION FUNCTIONS

Function

Description

Example

Returns

float( x )

Returns a floating-point value by converting x

float("10.0")

10.0

int( x )

Returns an integer value by converting x

int("10")

10

str( x )

Returns a string value by converting x

str(10)

'10'

### Using Augmented Assignment Operators

Augmented assignment operatorsis a mouthful. But the concept is simple. Let's say you want to know the yearly amount the user spends on food. To calculate and assign the yearly amount, you could use the line

```food = food * 52
```

This line multiplies the value of food by 52 and then assigns the result back to food . You could accomplish the same thing with this following line:

```food *= 52
```

*= is an augmented assignment operator. It also multiplies the value of food by 52 and then assigns the result back to food , but it's shorter than the first version. Since assigning a new value to a variable based on its original value is something that happens a lot in programming, these operators provide a nice shortcut to a common task. There are other augmented assignment operators. Table 2.6 summarizes some useful ones.

Table 2.6: USEFUL AUGMENT ASSIGNMENT OPERATORS

Operator To

Example

Is Equivalent

*=

x *= 5

x = x * 5

/=

x /= 5

x = x / 2

%=

x %= 5

x = x % 5

+=

x += 5

x = x + 5

-=

x -= 5

x = x - 5

### Printing Strings and Numbers Together

The next line of code

```print "\nGrand Total: ", total
```

is only slightly different than the corresponding line in the Trust Fund Buddy-Bad program:

```print "\nGrand Total: " + total
```

But the difference is an important one. In the Trust Fund Buddy-Bad program, the string "\nGrand Total: " and the value of total are joined together by string concatenation through the + operator. That's great because both are strings. However, in the Trust Fund Buddy-Good program, the value of total is an integer. So string concatenation won't work. Instead, the values are listed, separated by a comma. In general, you can list values separated by commas in a print statement to have them all print out together.