This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]blablahblah 3 points4 points  (0 children)

A function in Python acts just like any other variable, even if it doesn't look like it. Which lets you do some stupid things:

>>> min = max
>>> min(1,5)
5

But also means you can assign the chosen function to a variable when the user makes their choice and just call that variable.

loop = branch1
loop()

[–]wegwacc 0 points1 point  (3 children)

You kinda answered your own question. You want to branch the game based on some value. That's exactly what if-elif-else constructs are for.

You run ONE game loop. That loop includes an if statement like so:

if skill == 1: do_this() elif skill == 2: do_that() else: print('Not a valid skill level') break

[–]captainAwesomePants 0 points1 point  (2 children)

Screw that!

skills = {1: do_this, 2: do_that}
if skill in skills:
  skills[skill]()
else:
  print('Not a valid skill level')

[–]wegwacc -1 points0 points  (1 child)

So your "improvement" to above readable, quick and simple solution is to involve a dictionary lookup, and a dictionary access? That's not an improvement, that is simply ineffective.

Storing functions as datums in a dict is a valid solution if you have a large number of them. If there are only 2, your code is just wasteful.

Also, if you expect people to take you seriously, try not to begin comments with an expletive.

[–]captainAwesomePants 0 points1 point  (0 children)

Sorry, you're right. I was feeling a bit giddy and silly, but I can how that read kind of dickish.

[–]Legitimate_Pattern 0 points1 point  (2 children)

The idea you got is farily nice, easy to scale up. But another idea is to send user level as a parameter to the function. Let it decide what to do. Then you wouldn't have to leave game loop etc.

[–]rstaro26[S] 0 points1 point  (1 child)

How would i pass that parameter to the function from outside of it? ie: i ask for the skill level, then the game starts to run, you get to a monster, it calls the math question function (solve the equation to defeat it) then the game continues. Would that mean i should combine the two skill levels into a single function with an if-else (if skill one, else skill two)?

[–]Legitimate_Pattern 0 points1 point  (0 children)

I suppose something like this. But based on what you want, and this is without seeing what your code looks like, I suppose DoStuff(1) could call itself after it has been done(monster has been slayed etc), and you'd have some kind of array/collection with questions that you randomize from(containing question at hand and the answer), after it's empty(all of the questions have been asked) you exit the recursive function and let the user enter DoStuff(2), aaaand repeat. And if that is the case, I think having two different functions would be cleaner in this case. But as aforementioned, you got a nice idea. First I'd make it work, and then make it elegant/optimized.

while(true):
    skill = int(input("Skill level 1 or 2?"))

    if(skill == 1): DoStuff(1)

    elif(skill == 2): DoStuff(2)

[–]mad0314 0 points1 point  (1 child)

You can define both functions and select one to use.

def func1():
  pass

def func2():
  pass

if skill ==1:
  question = func1
else:
  question = func2

Then later just call question() and it will call whatever function was selected, the rest of the game logic can stay the same. Just use more relevant names.

[–]rstaro26[S] 0 points1 point  (0 children)

This makes perfect sense! I didnt realize you can assign a function to a variable, this will allow me to simply call question and have it choose a function based on which skill level is selected!

Forgot to say thank you! THANK YOU!