1. Python Basics1. Python Basics\1.2 How Programs Are Written\1.2.4 Getting User Input

1.2.4 Getting User Input

 

Almost every program you have ever used as a user has been interactive, right? A critical part of programming is interacting with users, and to do this, we need to get user input.

 

There are many ways of doing this, some simple, some advanced, but the easiest one to start with is asking the user to type something at a prompt.

 

Create a new Python file, type the following and run it. When you run it, unlike all our previous programs, it will not finish instantly. Instead, the Python interpreter will give you a prompt, and wait for you to type something, before finishing.

 

  1. print "I am the polite conversation bot."  
  2.   
  3. user_answer = raw_input("What do you think? ")  
  4.   
  5. print "Hmm,", user_answer, "... that is interesting."  
  6. print "... I agree!"  

 

At first you should get the output:

 

I am the polite conversation bot.

What do you think?

 

Then, Python should stop and wait for you to give it an answer. Here things get interesting, because the rest of the output depends on what you type. For instance, if you type, "turtles are quite well protected", this should be the remaining output:

 

turtles are quite well protected

Hmm, turtles are quite well protected ... that is interesting.

... I agree!

 

Your output now depends on your input.

 

In this text, user input is highlighted in green.

 

In line 3 of the above program, we introduce another Python command called raw_input. raw_input gets input from the user, and whatever the user enters gets put into the variable user_answer.

 

 

TIP: You could use another variable name here! There is nothing special about the variable name user_answer. We could have called the variable what_you_said, or almost anything else.

 

 

The string, "What do you think? ", is the "prompt" that raw_input will show to the user. You can change this prompt to anything you like, or leave it blank:

 

  1. answer = raw_input()

 

This will get input from the user without displaying any prompt.

 

So, why "raw" input? Python can do a lot of processing on the input, converting it to numbers for calculation, splitting up a sentence into individual words, or other things. So in this case, "raw" means unprocessed. Python treats whatever you enter as a string. Even if you enter a number, Python will just treat it as a string.

 

 

TIP: strings and numbers are fundamentally different. The string "5.25" is different from the number 5.25. Python can do math calculations with the number, but not with the string.

 

 

So, of course, we want to be able to get numbers from the user, not just strings.

 

Create a new Python file, type the following and run it:

 

  1. user_number_string = raw_input("Tell me a whole number, and I will double it: ")  
  2. user_number = int(user_number_string)  
  3.   
  4. print "Double your number is", user_number * 2
  5.   
  6. user_decimal_string = raw_input("Tell me a decimal number and I will half it: ")  
  7. user_decimal = float(user_decimal_string)  
  8.   
  9. print "Half your number is", user_decimal / 2.0  

 

When you run this, enter "56" for the first input, and "3.456" for the second input. You should see the output:

 

Tell me a whole number, and I will double it: 56

Double your number is 112

Tell me a decimal number and I will half it: 3.456

Half your number is 1.728

 

 

 

int is another new command. It turns a string of text into an integer (a whole number). So int("35") = 35. Similarly, float is a new command that turns a string of text into a "floating point" number. Floating point numbers are Python's way of representing decimals. Both int and float will give errors if they are given strings that can't be converted into numbers, so use them carefully.

 

 

TIP: Python also has a command called input that attempts to convert strings into numbers. However, it is now considered unsafe, and most Python programmers recommend never using it.

 

 

 

Top of Page