McBroColi
McBroColi

Reputation: 17

Python - I have previously defined variables yet they do not work in one function

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

Upvotes: 1

Views: 191

Answers (1)

user1632861
user1632861

Reputation:

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()

Upvotes: 2

Related Questions