0

I'm creating a text game so here is the point where I got stuck at:

def characters():

    def assassin():
        dmg = 150
        health = 500

print "Do you want to become an assassin ?"
choice = raw_input("> ")

if choice in ["Yes", "yes", "YES"]:
    print "So you said %s about becoming an assassin" % choice
    choice = assassin() # Error
else:
    print "Templar detected!"

So I wanted to set the choice variable to the assassin() function and then I wanted to copy the properties of the assassin() function to the choice variable but all I get is error that says " assassin is not defined.".

So, how can I do that ?

James Dunn
  • 8,064
  • 13
  • 53
  • 87
Marwan
  • 11
  • 6
  • See [How do I test one variable against multiple values?](http://stackoverflow.com/q/15112125); your `choice` test won't work as written. – Martijn Pieters Nov 27 '14 at 21:30
  • Your `characters` function doesn't do anything. Did you mean to have all the rest of the code inside `characters` as well? – BrenBarn Nov 27 '14 at 21:30
  • Is this supposed to be OOP? Perhaps look at https://docs.python.org/2/tutorial/classes.html – jonrsharpe Nov 27 '14 at 21:31
  • The local variables in your `assassin` function are not going to be assessable, apart from the fact that that function is inside another function that you didn't call. – Martijn Pieters Nov 27 '14 at 21:31
  • The error is obvious when you read the documentation - the part about name resolution in Python. `assassin` is a local function in `characters` and thus is visible only inside that function. Beside that, it is completely unclear what are you trying to achieve. – BartoszKP Nov 27 '14 at 21:31
  • You could use a class for that kind of thing however, but you shouldn't put that class inside a function still. – Martijn Pieters Nov 27 '14 at 21:31
  • Oh yeah, perhaps you typed `def` when you wanted `class`? – BartoszKP Nov 27 '14 at 21:32
  • 1
    Your if statement could use some attention. `if choice.upper() == "YES"` would take care of all case issues. – User Nov 27 '14 at 21:33
  • @BrenBarn my `characters` function will include many functions inside of it – Marwan Nov 27 '14 at 21:36
  • @jonrsharpe I'm learning python from a book called **Learn python the hard way** so i didn't get to the OOP part, I'm limited to functions only – Marwan Nov 27 '14 at 21:37
  • @MartijnPieters so i must call the 'Characters' function first then call 'assassin' function ? – Marwan Nov 27 '14 at 21:38
  • @BartoszKP I'm trying to organize the code so that function 'characters' will include many kinds of characters including 'assassin' function i was testing the code to make sure that everything is alright and then i would continue coding – Marwan Nov 27 '14 at 21:40
  • 3
    @Marwan Please re-read the chapter about functions and their purpose. You seem to have misunderstood it, and it's really hard to guess what are you trying to achieve exactly with these functions. The task you're solving doesn't really need local functions, and your functions don't do anything. We really would like to help, but there seems we would even have trouble finding some common ground to communicate. – BartoszKP Nov 27 '14 at 21:40
  • @User Thank you i will use that function now – Marwan Nov 27 '14 at 21:40
  • @Marwan: `characters` doesn't do anything with the `assassin` function; neither function returns anything. – Martijn Pieters Nov 27 '14 at 21:41
  • @BartoszKP ok Thank you i will re-read the chapter – Marwan Nov 27 '14 at 21:43
  • @MartijnPieters so if i did delete the `characters` function would the code work then ? – Marwan Nov 27 '14 at 21:45

2 Answers2

2

You have two problems:

if choice == "Yes" or "yes" or "YES": doesn't do what you think it does.

In Python, x or y always returns True or False based on the values of x and y. The Python intepreter inteprets that line different to how it would be intepreted in English.

It means the same as this if (choice == "Yes") or ("yes") or ("YES"). As a non-empty string has the Boolean value True, this is the same as if (choice == "Yes") or True or True. The first part of that - (choice == "Yes") or True has True as an argument to the or, so it (and therefore the whole line) will always be True.

The second problem is that you are misunderstanding how functions work. Firstly, neither of these functions do anything. A function is a way of giving a name to some code, which makes your program shorter if you are calling it more than once.

Secondly, variables in a function definition (and functions defined inside others) aren't accessible outside the function. That is why the variable assassin isn't located outside the definition of the characters function.

rlms
  • 10,650
  • 8
  • 44
  • 61
0

The assassin function doesn't do anything other than assigning the variables dmg and health some values.

Magnus N
  • 1
  • 1
  • 2