我正在使用 Python 3.6(32 位)创建基于文本的战斗机程序并将函数更改为字典

I'm creating a text based fighter program with Python 3.6 (32-bit) and changing a function to a dictionary

所以,这就是我想做的事情。

这是我的代码:

def weapon(): 
    '''A list of the available weapon'''
    print("Here is your weapon choices for today: ")
    print("1. Flail:\n   Damage: 1,6 \n    Heal: 1, 3")
    print("2. Dagger:\n   Damage: 2, 4 \n    Heal: 2, 3")
    print("3. Sword:\n   Damage: 1, 7 \n    Heal: 1, 2")
    print("4. Crossbow:\n   Damage: 0, 10 \n    Heal: 0, 0")
    print("5. Mace:\n   Damage: 1, 8 \n    Heal: 0, 0")
    print("6. Quarterstaff:\n Damage 3,10 \n Heal 0,5")

# Covert to integer to select weapon:

WEAPON = int(input("Choose your weapon: \n 1: Flail \n 2: Dagger,"
                   "\n 3: Sword,"
                   "\n 4: Crossbow, \n 5: Mace,"
                   "\n 6: Quarterstaff \n"
                   "Press the corresponding number for your weapon:\n "))

我想将函数更改为字典或 class 以便此字符串格式可以工作:

print('You choice a,'
      '{WEAPON} with {DAMAGE} and'
      '{HEAL} capabilties'.format(WEAPON=weapon, DAMAGE=Damage, HEAL=Heal))

我还会添加一条确认消息并允许用户选择其他武器。

编辑

这是完整的代码

from random import randint
NAMES = ["Ryu", "Bruce Lee", "Jet Li", "Steven Segal", "Batman",
         "The Flash", "Chuck Norris", "Frieza",
         "Cell", "Wonder Woman", "Goku", "Vegeta"]
NAMEINT = randint(0, 11)

if NAMEINT == 0:
    FIGHTERNAME = NAMES[0]
elif NAMEINT == 1:
    FIGHTERNAME = NAMES[1]
elif NAMEINT == 2:
    FIGHTERNAME = NAMES[2]
elif NAMEINT == 3:
    FIGHTERNAME = NAMES[3]
elif NAMEINT == 4:
    FIGHTERNAME = NAMES[4]
elif NAMEINT == 5:
    FIGHTERNAME = NAMES[5]
elif NAMEINT == 6:
    FIGHTERNAME = NAMES[6]
elif NAMEINT == 7:
    FIGHTERNAME = NAMES[7]
elif NAMEINT == 8:
    FIGHTERNAME = NAMES[8]
elif NAMEINT == 9:
    FIGHTERNAME = NAMES[9]
elif NAMEINT == 10:
    FIGHTERNAME = NAMES[10]
elif NAMEINT == 11:
    FIGHTERNAME = NAMES[11]

# Intro Text
NAME = input("Hello what is your name?\n")
print("Hello", NAME, "Today you will be fighter", FIGHTERNAME)

# Starting Health
PLAYERHP = 10
ENEMYHP = 10

# Weapon Inventory


def weapon():
    '''A list of the available weapon'''
    print("Here is your weapon choices for today: ")
    print("1. Flail:\n   Damage: 1,6 \n    Heal: 1, 3")
    print("2. Dagger:\n   Damage: 2, 4 \n    Heal: 2, 3")
    print("3. Sword:\n   Damage: 1, 7 \n    Heal: 1, 2")
    print("4. Crossbow:\n   Damage: 0, 10 \n    Heal: 0, 0")
    print("5. Mace:\n   Damage: 1, 8 \n    Heal: 0, 0")
    print("6. Quarterstaff:\n Damage 3,10 \n Heal 0,5")


# Covert to integer to select weapon
WEAPON = int(input("Choose your weapon: \n 1: Flail \n 2: Dagger,"
                   "\n 3: Sword,"
                   "\n 4: Crossbow, \n 5: Mace,"
                   "\n 6: Quarterstaff \n"
                   "Press the corresponding number for your weapon:\n"))


# Confirming Weapon Choice
print('You choice a,'
      '{WEAPON} with {DAMAGE} and'
      '{HEAL} capabilties'.format(WEAPON=weapon, DAMAGE=Damage, HEAL=Heal))

def confirm(prompt=None, resp=False):
    """prompts for yes or no response from the user. Returns True for yes and
    False for no.
    """

    if prompt is None:
        prompt = 'Confirm Weapon Choice'

    if resp:
        prompt = '%s [%s]|%s: ' % (prompt, 'y', 'n')
    else:
        prompt = '%s [%s]|%s: ' % (prompt, 'n', 'y')

    while True:
        ans = input(prompt)
        if not ans:
            return resp
        if ans not in ['y', 'Y', 'n', 'N']:
            print ('please enter y or n.')
            continue
        if ans == 'y' or ans == 'Y' or ans == 'Yes':
            return True
        if ans == 'n' or ans == 'N' or ans == 'No':
            return False

# Combat System Loop with weapons
while True:
    if WEAPON == 1:
        DAMAGE = randint(1, 6)
        HEAL = randint(1, 3)
    elif WEAPON == 2:
        DAMAGE = randint(2, 4)
        HEAL = randint(2, 3)
    elif WEAPON == 3:
        DAMAGE = randint(1, 7)
        HEAL = randint(1, 2)
    elif WEAPON == 4:
        DAMAGE = randint(0, 9)
        HEAL = randint(0, 0)
    elif WEAPON == 5:
        DAMAGE = randint(0, 9)
        HEAL = randint(0, 0)
    elif WEAPON == 6:
        DAMAGE = randint(3, 10)
        HEAL = randint(0, 5)
    NEWDAMAGE = randint(0, 6)

    print("the enemies hp is:", ENEMYHP)
    print("your hp is:", PLAYERHP)
    CHOICE = input("Would you like to attack or heal (ATT/HEA)")
    if CHOICE == "ATT":
        ENEMYHP = ENEMYHP - DAMAGE
        PLAYERHP = PLAYERHP - NEWDAMAGE
    if ENEMYHP <= 0:
        print("Well done you defeated", FIGHTERNAME)
        break
    elif PLAYERHP <= 0:
        print("You were defeated by", FIGHTERNAME)
        break
    if CHOICE == "HEA":
        PLAYERHP = PLAYERHP + HEAL
        PLAYERHP = PLAYERHP - NEWDAMAGE
    if ENEMYHP <= 0:
        print("Well done you defeated", FIGHTERNAME)
        break
    elif PLAYERHP <= 0:
        print("You were defeated by", FIGHTERNAME)
        break

ENDGAME = input("Press Enter to end the game")

编辑 2 这是我更新的代码

from random import randint
NAMES = ["Ryu", "Bruce Lee", "Jet Li", "Steven Segal", "Batman",
         "The Flash", "Chuck Norris", "Frieza",
         "Cell", "Wonder Woman", "Goku", "Vegeta"]
NAMEINT = randint(0, 11)

if NAMEINT == 0:
    FIGHTERNAME = NAMES[0]
elif NAMEINT == 1:
    FIGHTERNAME = NAMES[1]
elif NAMEINT == 2:
    FIGHTERNAME = NAMES[2]
elif NAMEINT == 3:
    FIGHTERNAME = NAMES[3]
elif NAMEINT == 4:
    FIGHTERNAME = NAMES[4]
elif NAMEINT == 5:
    FIGHTERNAME = NAMES[5]
elif NAMEINT == 6:
    FIGHTERNAME = NAMES[6]
elif NAMEINT == 7:
    FIGHTERNAME = NAMES[7]
elif NAMEINT == 8:
    FIGHTERNAME = NAMES[8]
elif NAMEINT == 9:
    FIGHTERNAME = NAMES[9]
elif NAMEINT == 10:
    FIGHTERNAME = NAMES[10]
elif NAMEINT == 11:
    FIGHTERNAME = NAMES[11]

# Intro Text
MYNAME = input("Hello what is your name?\n")
print("Hello", MYNAME, "Today you will be fighter", FIGHTERNAME)

# Starting Health
PLAYERHP = 10
ENEMYHP = 10

# Weapon Inventory


def weapon():
    '''A list of the available weapon'''
    print("Here is your weapon choices for today: ")
    print("1. Flail:\n   Damage: 1,6 \n    Heal: 1, 3")
    print("2. Dagger:\n   Damage: 2, 4 \n    Heal: 2, 3")
    print("3. Sword:\n   Damage: 1, 7 \n    Heal: 1, 2")
    print("4. Crossbow:\n   Damage: 0, 10 \n    Heal: 0, 0")
    print("5. Mace:\n   Damage: 1, 8 \n    Heal: 0, 0")
    print("6. Quarterstaff:\n Damage 3,10 \n Heal 0,5")


# Covert to integer to select weapon
WEAPON = int(input("Choose your weapon: \n 1: Flail \n 2: Dagger,"
                   "\n 3: Sword,"
                   "\n 4: Crossbow, \n 5: Mace,"
                   "\n 6: Quarterstaff \n"
                   "Press the corresponding number for your weapon:\n"))

if WEAPON == 1:
    NAME = Flail
    DAMAGE = randint(1, 6)
    HEAL = randint(1, 3)
elif WEAPON == 2:
    NAME = Dagger
    DAMAGE = randint(2, 4)
    HEAL = randint(2, 3)
elif WEAPON == 3:
    NAME = Sword
    DAMAGE = randint(1, 7)
    HEAL = randint(1, 2)
elif WEAPON == 4:
    NAME = Crossbow
    DAMAGE = randint(0, 9)
    HEAL = randint(0, 0)
elif WEAPON == 5:
    NAME = Mace
    DAMAGE = randint(0, 9)
    HEAL = randint(0, 0)
elif WEAPON == 6:
    NAME = Quarterstaff
    DAMAGE = randint(3, 10)
    HEAL = randint(0, 5)

# Confirming Weapon Choice
print('You choice a,'
      '{NAME} with {DAMAGE} and'
      '{HEAL} capabilties'.format(NAME=NAME, DAMAGE=DAMAGE, HEAL=HEAL))


def confirm(prompt=None, resp=False):

    """Prompts for yes or no response from the user."""
    if prompt is None:
        prompt = 'Confirm Weapon Choice'

    if resp:
        prompt = '%s [%s]|%s: ' % (prompt, 'y', 'n')
    else:
        prompt = '%s [%s]|%s: ' % (prompt, 'n', 'y')

    while True:
        ans = input(prompt)
        if not ans:
            return resp
        if ans not in ['y' 'Y', 'n', 'N']:
            print('please enter y or n.')
            continue
        if ans in ['y', 'Y', 'Yes']:
            return True
        if ans in ['n', 'N', 'No']:
            return False

# Combat System Loop with weapons
while True:
    if WEAPON == 1:
        DAMAGE = randint(1, 6)
        HEAL = randint(1, 3)
    elif WEAPON == 2:
        DAMAGE = randint(2, 4)
        HEAL = randint(2, 3)
    elif WEAPON == 3:
        DAMAGE = randint(1, 7)
        HEAL = randint(1, 2)
    elif WEAPON == 4:
        DAMAGE = randint(0, 9)
        HEAL = randint(0, 0)
    elif WEAPON == 5:
        DAMAGE = randint(0, 9)
        HEAL = randint(0, 0)
    elif WEAPON == 6:
        DAMAGE = randint(3, 10)
        HEAL = randint(0, 5)
    NEWDAMAGE = randint(0, 6)

    print("the enemies hp is:", ENEMYHP)
    print("your hp is:", PLAYERHP)
    CHOICE = input("Would you like to attack or heal (ATT/HEA)")
    if CHOICE == "ATT":
        ENEMYHP = ENEMYHP - DAMAGE
        PLAYERHP = PLAYERHP - NEWDAMAGE
    if ENEMYHP <= 0:
        print("Well done you defeated", FIGHTERNAME)
        break
    elif PLAYERHP <= 0:
        print("You were defeated by", FIGHTERNAME)
        break
    if CHOICE == "HEA":
        PLAYERHP = PLAYERHP + HEAL
        PLAYERHP = PLAYERHP - NEWDAMAGE
    if ENEMYHP <= 0:
        print("Well done you defeated", FIGHTERNAME)
        break
    elif PLAYERHP <= 0:
        print("You were defeated by", FIGHTERNAME)
        break

ENDGAME = input("Press Enter to end the game")

现在,它表示名称未定义,所以我正在考虑更改

WEAPON = int(input("Choose your weapon: \n 1: Flail \n 2: Dagger,"
                   "\n 3: Sword,"
                   "\n 4: Crossbow, \n 5: Mace,"
                   "\n 6: Quarterstaff \n"
                   "Press the corresponding number for your weapon:\n"))

以武器名称为键,伤害和治疗为值的字典。有没有办法做到这一点。 IE。有一个带有 1 个键的字典,它有 2 个随机生成的值?

PS。我使用 pycodestyle 和 pylint 进行格式化和 PEP8 检查。

更新。

现在基本上可以用了。我能够完成一个游戏循环,但是,确认循环不起作用。假设发生的是你收到确认你的武器的警告,但如果你不喜欢它,你可以返回到武器选择。

def confirm(prompt=None, resp=False):

    """Prompts for yes or no response from the user."""
    if prompt is None:
        prompt = 'Confirm Weapon Choice'

    if resp:
        prompt = '%s [%s]|%s: ' % (prompt, 'y', 'n')
    else:
        prompt = '%s [%s]|%s: ' % (prompt, 'n', 'y')

    while True:
        ans = input(prompt)
        if not ans:
            return resp
        if ans not in ['y' 'Y', 'n', 'N']:
            print('please enter y or n.')
            continue
        if ans in ['y', 'Y', 'Yes']:
            return True
        if ans in ['n', 'N', 'No']:
            return False

confirm()

现在,我确信我错过了一些东西,比如如何将它循环回到之前的选择,只是不知道该怎么做。

您的字符串格式不起作用并且出现错误的原因是您使用未定义的变量定义了 NAME 变量。将你所有的武器名称用语音标记包裹起来,使它们成为字符串。

if WEAPON == 1:
    NAME = 'Flail'      #Change this to a string
    DAMAGE = randint(1, 6)
    HEAL = randint(1, 3)
elif WEAPON == 2:
    NAME = 'Dagger'      #and this
    DAMAGE = randint(2, 4)
    HEAL = randint(2, 3)
elif WEAPON == 3:
    NAME = 'Sword'      #and this
    DAMAGE = randint(1, 7)
    HEAL = randint(1, 2)
elif WEAPON == 4:
    NAME = 'Crossbow'      #and this etc...
    DAMAGE = randint(0, 9)
    HEAL = randint(0, 0)

你可以在这里清理一些东西,但这里有一个建议..改变这个;

if NAMEINT == 0:
    FIGHTERNAME = NAMES[0]
elif NAMEINT == 1:
    FIGHTERNAME = NAMES[1]
elif NAMEINT == 2:
    FIGHTERNAME = NAMES[2]
elif NAMEINT == 3:
    FIGHTERNAME = NAMES[3]
elif NAMEINT == 4:
    FIGHTERNAME = NAMES[4]
elif NAMEINT == 5:
    FIGHTERNAME = NAMES[5]
elif NAMEINT == 6:
    FIGHTERNAME = NAMES[6]
elif NAMEINT == 7:
    FIGHTERNAME = NAMES[7]
elif NAMEINT == 8:
    FIGHTERNAME = NAMES[8]
elif NAMEINT == 9:
    FIGHTERNAME = NAMES[9]
elif NAMEINT == 10:
    FIGHTERNAME = NAMES[10]
elif NAMEINT == 11:
    FIGHTERNAME = NAMES[11]

到此;

FIGHTERNAME = NAMES[NAMEINT]

它会做同样的工作。您还应该仔细检查 PEP8,因为它应该更像 fighter_name = names[nameint].