Storing a Variable


#1

Evening All :slight_smile:

Just working my way through the python course, and wanted to take a break to play around with what I have so far. So creating a very basic text game and am stuck trying to re-use a variable on a later line.
In the first instance I want the variable char_name to be declared by the player, however I would like that raw_input stored so I can re-use the players name throughout.

Can I somehow tell python that the input is to be assigned to char_name permanently or do I need to create another variable and make itโ€™s value = to the raw_input and if so how do I go about this?

P.S #This is my first attempt at making any sort of code so apologies if itโ€™s horribly written, I realize that there will be more efficient ways to do this which I will learn in time.

Thanks in advance.
sandr3w


from time import sleep
def TheGame():
    print "What fool dares enter my dungeon!!?"
    char_name = raw_input("Enter your name: ")
    if len(char_name) > 0 and char_name.isalpha():
        print "Well, well, well %s. This is no place for the weak." % (char_name)
        print "Do you truly believe a mortal creature like you will leave this dungeon?" ;sleep(2)
    else: 
        print "YOU WILL GIVE ME YOUR NAME!!!" ;sleep(2)
        TheGame()        
    brave = raw_input ("Well are you brave enough to begin %s?") % (char_name) 
TheGame()
 

#2

Type mismatch. Did you mean to call the len() function?


#3

I did, thank you for pointing that out mtf.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.