Quantcast

Jump to content


Photo

Question regarding storing user-input as a variable.


  • Please log in to reply
5 replies to this topic

#1 jaredennisclark

jaredennisclark
  • 838 posts

Posted 21 October 2010 - 12:50 PM

I'm about two days into learning a bit of Python and I have a question.

I'm trying to capture user input and store it as a variable for later use. Here is something along the lines of what I have right now:

prompt = "What is your name?\n"
name = raw_input(prompt)

So from this I'll have the person's name, stored under the variable 'name'. Now what I want to do is take that variable and insert it into a string so that the output from the program will say something like:

"Hello Jared! How are you today?"


Except it doesn't necessarily need to say 'Jared'. I want 'Jared' to be replaced with whatever input the user gave and stored under the variable 'name'.

I was tinkering with something like the following:

print 'Hello,' name '! How are you today?'

That obviously didn't work. :p How would you all go about doing something like this? And WHY didn't my little snippet above work at all?

#2 Waser Lave

Waser Lave

  • 25516 posts


Users Awards

Posted 21 October 2010 - 12:52 PM

print 'Hello,'+str(name)+'! How are you today?'


#3 Pyro699

Pyro699
  • 1543 posts


Users Awards

Posted 21 October 2010 - 12:56 PM


print 'Hello, %s! How are you today?' % name


If you had more than one input to display you could do


print 'Hello, %s! Some other input goes here: %s' % (name, otherVar)


Check this tutorial out :)
http://docs.python.o...nputoutput.html

Edited by Pyro699, 21 October 2010 - 12:56 PM.


#4 jaredennisclark

jaredennisclark
  • 838 posts

Posted 21 October 2010 - 12:59 PM

Awesome. Thank you both. ;)

#5 xavarn

xavarn
  • 109 posts

Posted 21 October 2010 - 01:33 PM

Just wondering what language is this in?

#6 Waser Lave

Waser Lave

  • 25516 posts


Users Awards

Posted 21 October 2010 - 01:34 PM

Just wondering what language is this in?


Python.


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users