1

At the very beginning of the Python Script, I have defined a lot of variables. For instance:

cmd_attack = "attack"
cmd_protect = "protect"
cmd_help = "help"

cmd_help works in a user menu function shown here:

def usermenu():
    um_in=raw_input('Menu :: ')
    #Help Command
    if um_in.lower()==cmd_help.lower():
        print(helplist)
        usermenu()

That is successful - it prints the help list and then returns to the raw input. However, when I do something similar involving cmd_help in another function...

def tf_elf_battle_merc():
    battleinput==raw_input('Elf :: ')
    global cmd_help
    global cmd_attack
    global cmd_protect
    if battleinput.lower()==cmd_attack.lower():
        attack_merc()
    elif battleinput.lower()==cmd_help.lower():
        print(tf_elf_help)

That does nothing, prints no errors, and returns to the shell line - without printing anything or going anywhere. I used global commands because I was testing out possible solutions to the problem. The order that these are put in is the CMD functions at the top, the tf_elf_battle_merc() in the middle, and the usermenu() last. I have tried a few things and the related questions haven't helped... any thoughts? I'm kind of new to Python. If you're curious, it is script where you can log in and play text-type games.

The full script is here on Pastebin.

Thank you in advance!

Edit: If you download and run the script - use "Guest" (case-sensitive) as a username and you'll be let into it

McBroColi
  • 17
  • 3
  • “That does nothing, prints no errors, and returns to the shell line - without printing anything or going anywhere.” – That means the code is probably never reached. What makes you think that it’s because of those global variables? Does it work if you leave them out? Try printing the function name at the beginning of each function to see how exactly the flow goes, the problem is probably somewhere else, and you might see where it stops that way. – poke Dec 01 '12 at 19:40
  • Will do and return the results – McBroColi Dec 01 '12 at 19:40
  • Btw. you do not need to `global varname` all those variables just to read them. It’s only needed if you want to change them (which you don’t) so you can, and should, leave those lines out. – poke Dec 01 '12 at 19:42
  • 1
    I can highly recommend to not use globals like this. It makes your code very hard to follow and very error prone. Pass data you need into the function via it's parameters, and give data back via the return statement. – Ikke Dec 01 '12 at 19:50

1 Answers1

2

Your code (with some edits, seen below) worked fine for me after changing battleinput==raw_input('Elf :: ') to battleinput=raw_input('Elf ::'), you don't want to compare them, you want to define battleinput.

However, it should raise an error of that, since battleinput is not defined, yet you're trying to comparing it: if battleinput.lower() == ....

Also you're mixing Python 3 and Python 2? Using raw_input() from Python 2, yet print("asd") from Python 3, instead of Python 2's print "asd"?

Everything looks like your code is never reached, the problem is elsewhere.


Here's the code for Python 3, which works fine:

cmd_attack = "attack"
cmd_protect = "protect"
cmd_help = "help"


def tf_elf_battle_merc():
    battleinput=input('Elf :: ') # Use raw_input() for Python 2
    # You don't need the globals here
    if battleinput.lower()==cmd_attack.lower():
        print("attack")
    elif battleinput.lower()==cmd_help.lower():
        print("help")

tf_elf_battle_merc()
  • I haven't actually used Python 3 and I'm trying to use code that I've sort of thought out from previous languages, which would describe the mixup. – McBroColi Dec 01 '12 at 19:43
  • `print(x)` is identical to `print x` in Python; with a single parameter at least. – poke Dec 01 '12 at 19:43
  • In python 3, `print()` is a function, and cannot be used without parenthesis. In Python 2, `print` is a statement, and doesn't therefore take parenthesis. Haven't tested `print("foo")` in Python 2 tho. –  Dec 01 '12 at 19:45
  • If I had more reputation, I'd vote you up - and it won't let me check your answer off for another few minutes. The problem was a typo, that I was using == instead of =. I'm not sure how I'm mixing around the code so much but it does work with =, which is what I meant. I kind of feel silly, and it seems like a stupid question now. – McBroColi Dec 01 '12 at 19:46
  • Accept the answer when you can. Just curious, what IDE are you using to compile and run this code? Python IDLE and Eclipse w/ Python both raised an error from undefined `battleinput`. It should've raised an error for you too. –  Dec 01 '12 at 19:47
  • I'm using the normal IDLE on 2.7.3 and it runs perfectly – McBroColi Dec 01 '12 at 19:49
  • That's weird, it should raise an error. I got 3.2.3, but I think it should be the same... I'd recommend getting a better IDE, but your call. –  Dec 01 '12 at 19:50
  • I've been looking for one. Is Eclipse very good? Other than that, do you have any other recommendations? – McBroColi Dec 01 '12 at 19:51
  • Eclipse works fine. You might wanna take a look at this: http://stackoverflow.com/questions/81584/what-ide-to-use-for-python However, I recommend eclipse with PyDev: http://pythoncentral.org/the-best-python-ides-you-can-use-for-development/#section-1 –  Dec 01 '12 at 19:54
  • Thank you, you have been very helpful. – McBroColi Dec 01 '12 at 19:56
  • @Mahi Sorry, meant to say “identical to `print x` in Python 2“. The parentheses are part of the expression and `(x)` is the same as `x`. – poke Dec 01 '12 at 20:14