Search code examples
pythonraw-inputstring.format

Python - issue with strings and .format()


I've just started to learn Python and I'm making a little game to practice what I've learnt. I've encountered a slight problem, though:

Here's the part that's not working:

name = raw_input("Enter Name: ")

print""

print "^^^^^^^^^^^^^^^^^^"

start_message = raw_input("Another day on Uranus, {name}!. Shall we go outside or stay within the protection bubble? (Press 'Enter')").format(name=name)

The output:

Enter Name: James


^^^^^^^^^^^^^^^^^^
Another day on Uranus, {name}!. Shall we go outside or stay within the protection bubble? (Press 'Enter')

Solution

  • You are formatting the raw_input not the string within the raw_input. You need to change where your parenthesis is:

    start_message = raw_input("Another day on Uranus, {name}!. Shall we go outside or stay within the protection bubble? (Press 'Enter')".format(name=name))
    

    Notice that I pulled the parenthesis from after the closing " to after the format and now it's all in raw_input()

    Output:

    Enter Name: Andy
    
    ^^^^^^^^^^^^^^^^^^
    Another day on Uranus, Andy!. Shall we go outside or stay within the protection bubble? (Press 'Enter')