2015-10-02 2 views
0

Je viens de commencer à apprendre Python et je fais un petit jeu pour mettre en pratique ce que j'ai appris. Je l'ai rencontré un léger problème, cependant:Python - problème avec les chaînes et .format()

est ici la partie qui ne fonctionne pas:

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) 

La sortie:

Enter Name: James 


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

Répondre

0

Vous formatez le raw_input pas la chaîne dans le raw_input. Vous devez changer où votre parenthèse est:

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

Notez que j'ai tiré la parenthèse de après la fermeture " à après la format et maintenant il est dans raw_input()

Sortie:

Enter Name: Andy 

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

Vous devez formater la chaîne que vous passez à raw_input:

......(Press 'Enter')".format(name=name) 

Vous essayez de formater l'entrée que vous prenez de raw_input.

1

Ah vous avez une erreur de parenthèse.

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

a besoin d'être

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

Ah Padraic Cunningham a ce – bseyeph

+0

le vote sur cesse donc jamais d'étonner, la dernière réponse 5 minutes après la première avec la même suggestion exacte est le seul upvoted –