[Tutor] Newbie question re: user interaction

Deirdre Saoirse deirdre@deirdre.net
Thu, 1 Feb 2001 13:32:41 -0800 (PST)

On Thu, 1 Feb 2001, First Name Last Name wrote:

> Anyway, what I'm writing to ask about is, how do I do the equivalent
> of the following BASIC function in Python?
> 10 INPUT "What is your name?" N$
> 20 PRINT "Hello, " N$ "!"

n = raw_input("What is your name?")
print "Hello, %s!" % n

The syntax of the two languages' input and print is different, but the
basic concept is the same.

> This is a staggeringly simple piece of code and I instinctively feel
> that a well-designed language like Python must have a simple and
> elegant way of replicating it.  However, all I've managed to gather
> from the manual is that if I do something horribly complicated with
> the cmd module then I might be able to do something that vaguely
> mimics the code above.  Please tell me I'm wrong!

I think you'll find that a lot more is the same than you might think. It's
the details that are different.

The %s means "put a string here, I'll tell you what it is later." The %
later in the line means "OK, here's the list of stuff to put in."