[Tutor] How do I call a variable within an input () function?

Mark Lawrence breamoreboy at yahoo.co.uk
Fri Feb 6 15:56:11 CET 2015


On 06/02/2015 07:20, Danny Yoo wrote:
> On Thu, Feb 5, 2015 at 3:57 PM, Edgar Figueroa <figgypops at hotmail.com> wrote:
>> Hello group.  I'm trying to call the variable "name" within my input () function.
>> Here's what I have:
>> name = input("Hello. What's your name? ")
>> print("\nHello", name, ". Nice to meet you.")
>> favFood1 = input("\n", name, ", what's your favorite food? ")
>> Obviously it isn't working.  It tells me I have too many arguments for the input ()
>
> Try not to paraphrase error messages.  Rather, copy-and-paste the
> error message exactly as the computer says.  This might seem counter
> to what you're used to doing, but it helps because sometimes the
> slightest detail helps to figure out what's going on.
>
> In particular, your program has *two* uses of input.  But the error
> message should have said which one was problematic, and I'm pretty
> sure it has done so!  But since you've paraphrased the message, we
> would not be able to tell which one was broken, even if the error
> message said exactly which one was wrong.  *That's* why you should
> avoid paraphrasing error messages.
>
> In any case, since there's only two, we can look and see that the
> problematic one is probably this:
>
>      favFood1 = input("\n", name, ", what's your favorite food? ")
>
> You want to pass input() a single string.  But you have a few strings there.
>
> Do you know about "string appending" or "string concatenation"?
>

String formatting should also be mentioned, old style using the % 
operator as in C or new style using {} and format.

favFood1 = input("\n%s what's your favorite food? " % name)
favFood1 = input("\n{} what's your favorite food? ".format(name))

I much prefer the latter as I find it far more flexible.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence



More information about the Tutor mailing list