Prompting People

When you typed raw_input(), you were typing the ( and ) characters, which are paren- thesis characters. This is similar to when you used them to do a format with extra vari- ables, as in "%s %s" % (x, y). For raw_input, you can also put in a prompt to show to a person so he knows what to type. Put a string that you want for the prompt inside the () so that it looks like this:

y = input("Name? ")

In [ ]:

This prompts the user with “Name?” and puts the result into the variable y. This is how you ask someone a question and get the answer.

This means we can completely rewrite our previous exercise using just raw_input to do all the prompting.

age = input("How old are you? ")
height = input("How tall are you? ")
weight = input("How much do you weigh? ")

print ("So, you're %r old, %r tall and %r heavy." % (age, height, weight))

In [ ]:

What You Should See

How old are you? 2 How tall are you? 3 How much do you weigh? 4 So, you're '2' old, '3' tall and '4' heavy.

Study Drills

  1. In Terminal, where you normally run python to run your scripts, type pydoc input. Read what it says. If you do not understand those; just read through and take notes about interesting things.

Common Student Questions

How come I get SyntaxError: invalid syntax whenever I run pydoc?

You aren’t running pydoc from the command line; you’re probably running it from inside python. Exit out of python first.

Why does my pydoc not pause like yours does?

Sometimes if the help document is short enough to fit on one screen, then pydoc will just print it.

When I run pydoc I get more is not recognized as an internal.

Some versions of Windows do not have that command, which means pydoc is broken for you. You can skip this Study Drill and just search online for Python documentation when you need it.

Why would I use %r over %s?

Remember, %r is for debugging and is “raw representation” while %s is for display. I will not answer this question again, so you must memorize this fact. This is the #1 thing people ask repeat- edly, and asking the same question over and over means you aren’t taking the time to memorize what you should. Stop now, and finally memorize this fact.

Why can’t I do print "How old are you?" , raw_input()?

You’d think that’d work, but Python doesn’t recognize that as valid. The only answer I can really give is, you just can’t.