Thursday, May 8, 2008

Accepting Inputs in Python

I want to learn how to get or accept inputs in python. I want to start from the very basic. In C, there are several functions you can choose from, the very basic is scanf(). In python, there is this raw_input() function. The raw_input([prompt]) function will first display what is placed in [prompt] then wait for keyboard input(s) and return the input as a string.

Now, lets try this example:

#!/usr/bin/env python
# Filename: greeter.py
# Description: A program that ask for a name and greet hello.
# Author: Obispo

# Ask for a name.
iname = raw_input("What's your name? ")
# Be courteous
print "Oh, hello " + iname + "! Glad to meet you!"

Here is the output of our greeter.py code:

# ./greeter.py
What's your name? Juan
Oh, hello Juan! Glad to meet you!

We have to take note that raw_input() will return the input as a string. Let us take a look at our second example. Suppose I want to ask two numbers and display the sum of those numbers? Let us try:

#!/usr/bin/env python
# Filename: simplemath.py
# Description: A program that takes two numbers and display the sum
# Author: Obispo

# Ask for the first number
num1 = raw_input("Enter first number: ")
# Ask for the second number
num2 = raw_input("Enter second number: ")
# Alright, let's display the sum
print "The sum of ", num1, " and ", num2, " is ", (num1 + num2)

I wanna see the output now!

# chmod +x simplemath1.py
# ./simplemath1.py
Enter first number: 5
Enter second number: 3
The sum of 5 and 3 is 53

Oh, I don't think the answer is correct. What happened? As I have said, raw_input() will return a string, so, when we type 5 and 3, num1 and num2 contains the string "5" and "3" respectively. What about (num1 + num2)? Would it not automatically convert num1 and num2 into a numerical value then add? Well, from the output of the program, it didn't add num1 and num2 but instead, it concatenate the two strings "5" + "3" equals "53"!

You will notice that in our first example (greeter.py), we print our message like this: "Oh, hello " + "Juan" + "! Glad to meet you!". The "+" operator will be used to concatenate two strings if our operands are strings but it can also be used to add two values if our operands are numeric values.

In order to get the desired result, I used the function float() in order to convert the input "5" into a numeric value 5.0 and the input "3" into a numeric value 3.0. You can also try to use other numeric conversion functions like int() and long().

#!/usr/bin/env python
# Filename: simplemath.py
# Description: A program that takes two numbers and display the sum
# Author: Obispo

# Ask for the first number
num1 = float(raw_input("Enter first number: "))
# Ask for the second number
num2 = float(raw_input("Enter second number: "))
# Alright, let's display the sum
print "The sum of ", num1, " and ", num2, " is ", (num1 + num2)

Let's see the output:

# ./simplemath.py
Enter first number: 5
Enter second number: 3
The sum of 5.0 and 3.0 is 8.0

Whew! At last, the output seems to be correct now. What I want to learn next is how to trap errors in python, try to explore the program and you will see why.


No comments: