python - NameError: name '<name>' is not defined, when using % format syntax for strings -


traceback (most recent call last):   file "c:\users\simon\downloads\rpgbs.py", line 72, in <module>     print("%(chara) has %(health) hp." % {chara:names[k], health:str(health[k])}) nameerror: name 'chara' not defined 

i above error when running code. relevant sections below.

when run print(names["enemy"], health["enemy"]) on own, seems work fine. when use formatting syntax (from understanding print("%(author) likes spam.") % {author:"simon"}), goes right down drain. have fact i'm trying iterate it?

names = {'player' : str(input("what's name, hero?: ")),                                                   'enemy' : str(input("and duelling with?"))} if names["enemy"]=="undertale":     names["enemy"]=namechoose(["froggit", "sans", "flowey", "chara", "asriel dreemur", "toriel", "undyne", "mettaton"],1) elif names["enemy"]=="pokemon":     names["enemy"]=namechoose(["charizard","blastoise", "venusaur", "arceus", "dialga", "palkia", "groudon"],1) elif names["enemy"]=="dragon quest":     names["enemy"]=namechoose(["corvus", "aquila", "hootingham-gore", "goresby-purrvis", "stella", "the almighty", "slime", "metal slime"],1)      startinghealth=int(input("how health wish start with? 150 recommended."))          health = {'player' : startinghealth,                                                                           'enemy' : startinghealth}                                                                    while health["enemy"]>0 , health["player"]>0:     k in names:         print(names[k], health[k])         print("%(chara) has %(health) hp." % {chara:names[k], health:str(health[k])})                                                                                         

you missed s character after brace, use chara variable in line not string key hash. why have error don't have chara variable defined. correct line should this: print("%(chara)s has %(health)s hp." % {'chara':names[k], 'health':str(health[k])})


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -