Friday, September 10, 2010

Input and Output in Python

Computers have three major operations, the sole reason they exist. These are input, processing, and output. If they computer couldn't do one of these it would become practically useless. You input data with every movement of the mouse and every button pressed on the keyboard. The computer processes that data into something, a movement of the cursor, a character typed, and outputs that data to the screen so you can see where the computer thinks the cursor is and what character you typed on the monitor. So far we have placed our variables and their values within the program itself so the user has no interaction with it. They click, it runs. Now let's try building an interactive program.

If you checked out the MIT OCW Introduction to Computer Science and Programming course you may have seen the first homework assignment was to create a program with the following requirements:

Problem 1.
Write a program that does the following in order:
1. Asks the user to enter his/her last name.
2. Asks the user to enter his/her first name.
3. Prints out the user’s first and last names in that order.

Before we start writing code, we can create pseudocode.

Start
input lastName
input firstName
output firstName lastName.
Stop

This would do the job, but how does the user know which name to put first or that you want them to enter a name at all? You can ask them, with:

Start
output "What is your Last Name? "
input lastName
output "What is your First Name? "
input firstName
output firstName lastName
Stop

This is better, but I think we can do even better. Here is the Flowchart and Psuedocode for a simple answer to that problem.




Start
output "What is your Last Name? "
input lastName
output "What is your First Name? "
input firstName
output "Is your name " firstName lastName ?
Stop






And finally in Python:

##This program asks for Last Name, then First Name and then gives
##First and Last name in that order.
lastName = input("What is your Last Name?")
firstName = input("What is your First Name?")
print("Is your name" , firstName , lastName ,"?")

*Note - Apparently raw_input() has been disabled for Python V3.0 (I have updated my code to reflect this). 

**Edit** Thanks to bernd-petersohn. Here is his Python3 code:

#!/usr/bin/python3
lastName = input("What is your Last Name? ")
firstName = input("What is your First Name? ")
print("Is your name {0} {1}?".format(firstName, lastName))

We still do not act on our data or have decisions, but you can input some data, the data is saved into variables, and the data is then output for your viewing pleasure. His next assignment gets much more difficult and I'll pose the question to you now to think on it... How would you design a program that finds and outputs the 1000th prime number?

7 comments:

  1. #!/usr/bin/python3
    lastName = input("What is your Last Name? ")
    firstName = input("What is your First Name? ")
    print("Is your name {0} {1}?".format(firstName, lastName))

    ReplyDelete
  2. Bernd, Thank you! I tested the code in both 2.7 and 3.1.2 and it did work in 3.1.2. I'm sure you knew it would :) But again, thank you.

    ReplyDelete
  3. I'm not so sure about that. For Python 2.x, I currently only have version 2.6.5 at hand. However, according to the documentation, in Python 2.7 input() still evaluates the input strings [as in eval(raw_input())]. Also, the print statement emits a tuple representation if given multiple arguments in parentheses. The single argument form print(s) works equally in both Python 2 and 3.

    ReplyDelete
  4. Oops - I misunderstood your comment at the first glance. Yes, my Python 3 version does not properly work in Python 2, except for the line printing the name, which works in both versions.

    ReplyDelete
  5. FYI, the page loaded via this URL:

    http://geekspot-101.blogspot.com/2010/09/input-and-output-in-python.html

    ...gets an error as a result of trying to pull something from here:

    ftp://a7986093@geekspot101.site88.net/ param name="jnlp_href" value="launch.jnlp">

    ReplyDelete
  6. the date of birth is the difficult part, retards!!!

    ReplyDelete

  7. lastname = raw_input('What is your last name? ')
    firstname = raw_input('What is your first name? ')
    print (firstname + (' ') +lastname)

    for the dob question
    birth = raw_input('What is your date of birth? ')
    name = raw_input('What is your last name? ')
    print (name + ', ' + birth)

    ReplyDelete