Operations on two numbers

We are going to use the raw_input() function that we learned about in Chapter 2, Variables, Functions, and Users. Recall that from this chapter, we cannot perform addition on two strings. In fact, we cannot perform any kind of mathematical operations on strings.

The following code asks for user input and stores the input in the computer as strings. Type the following code in your Python shell to take a look at the results:

def addition():
    first = raw_input('I will add two numbers. Enter the first number')
    second = raw_input('Now enter the second number.')
    print(first + second)

What happens when you call the addition() function? If you call the addition() function, you will see that the addition has NOT happened. This program just prints the two numbers together, side by side, in the order that they were entered by the user:

Operations on two numbers

While putting information side by side is useful to combine words into a phrase or sentence, it is not very helpful in performing calculations with numbers, as we discovered in Chapter 2, Variables, Functions, and Users. Instead, you will want to convert the user's answer to a number so that you can perform mathematical operations on the numbers. To convert the raw_input() function to a number, you will use int() or the float() functions.

Convert data into numbers – int() and float()

In order to change the user data entered in the raw_input() function from a string to a number, we need to use the whole-number-integer, int(), or floating-point-number, float(), functions to make the computer interpret the answer as a number.

Floating point to whole number conversion

To try an example, type the following in your Python shell, and pay attention to the results:

a = int(44.5)
b = float(44.5)
print(a)
print(b)

In the preceding example, with 44.5, you should notice that the int() function rounds up the number to 44, while the float() function keeps the number at 44.5. This is because int() likes whole numbers and rounds numbers down automatically. Take a look at this screenshot from the Python shell:

Floating point to whole number conversion

Whole number to floating point conversion

Now, try the reverse. Convert a whole number into an integer and a float using this code in your Python shell:

 a = int(24)
 b = float(24)
 print(a)
 print(b)

In the preceding sample code, you see that the int() function keeps the number at 24, while the float() function adds a decimal place to the number, making it print as 24.0. This is because float is designed to deal with numbers and their decimal places. You can see the results in this screenshot of the Python shell:

Whole number to floating point conversion

Text strings fail in int() and float()

If you try to enter a text string into the int() or float() functions, you will get an error. In fact, you will only be able to type the first line of these two lines into your Python shell. This will immediately evaluate the int('hello') code as an error:

    int('hello')
    float('hello')

This happens because int() and float() apply specifically to numbers and do not deal with things that cannot be changed into numbers. In the following screenshot, notice that the Python shell returns something called a traceback with three lines of error code:

Text strings fail in int() and float()

We will switch between using int() and float() throughout the book so that you become used to using both functions:

  • int(): This function converts data into a whole number
  • float(): This function converts data into a number with decimal places

Now that we know about converting strings into numbers, let's rewrite our addition function, get input from the user, and convert the input into decimal numbers using the float() function. You can copy this code directly into your text editor:

  def addition():
    first = float(raw_input('What is your first number?'))
    second = float(raw_input('What is your second number?'))
    print(first + second)

In the following screenshot, you see the Python shell with the addition function defined. You also see that when the addition function is called, each raw_input line is printed and the user answers by typing in a number. The first and second input have been converted into integers, so when the answer is added together you will notice that the output is now correct according to the standard rules of addition:

Text strings fail in int() and float()

Creating our first calculator file

Let's save your work before we continue. Open your text editor and make a file called first_calc.py, then type the addition function that you just made into that file. Make sure you save the file in your work folder that you made on your desktop back in Chapter 1, Welcome! Let's Get Started. It is important to keep your work organized so that you can run your code to test it and show it off:

Creating our first calculator file
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.188.16