我不知道如何在我的游戏中显示 类 列表供用户选择?

I don't know how to show a list of classes for user to choose from in my game?

我正在制作一款名为 Glaux 的文字冒险游戏。这个游戏是一个RPG游戏,但它是在文字上。 我的问题是如何向用户 3 类 显示他可以通过键入一个选项来进行选择,但我不知道该怎么做。(下面提供的代码)。

import random
class Warrior:
    def __init__(self, name, health, steel_armor, healing_potion, attack, rage):
        self.name = input(print("Enter your name, Warrior!"))
        self.health = health
        self.steel_armor = steel_armor
        self.healing_potion = healing_potion
        self.attack = attack
        self.rage = rage
class Mage:
    def __init__(self, name, health_magic, magic_armor, magic_heal, magic_attack, mana):
        self.name = input(print("Enter your name, Mage!"))
        self.health = health_magic
        self.magic_armor = magic_armor
        self.magic_heal = magic_heal
        self.magic_attack = magic_attack
        self.mana = mana
class Archer:
    def __init__(self, name, health_spirit, elevens_armor, spiritual_heal, spiritual_attack, spirit):
        self.name = input(print("Enter your name, Archer!"))
        self.health = health_spirit
        self.elevens_armor = elevens_armor
        self.spiritual_heal = spiritual_heal
        self.spiritual_attack = spiritual_attack
        self.spirit = spirit
def Classes(Warrior, Mage, Archer):
        Classes = (Warrior, Mage, Archer)
Play = input

run = True


main_menu = True
while run:
    while main_menu:
        question = input("To play enter [Play]: ")
        if question != 'Play':
                print("Please enter a valid option")
                continue
        else:
            break
    print("you were a....."), # here i want to begin the part after main_menu the game will ask the user to choose between 3 classes so how to show the user those 3 classes ?
    print("Choose your class!")
    input(Classes('Warrior', 'Mage', 'Archer'))

    break

你可以这样做:

你定义了一个方法类,它正在做一些奇怪的事情,我不知道你的意图是什么,但你应该改变:

def Classes(Warrior, Mage, Archer):
    Classes = (Warrior, Mage, Archer)

变成这样的东西:

classes = ["Warrior", "Mage", "Archer"]

包含所有可用 classes.

的列表

之后你可以让玩家选择他的 class 像这样:

player_class = input(classes)
    while player_class not in classes:
        player_class = input(classes)
    print("You are: {}".format(player_class))

你可能应该让输出更漂亮一点,但它适用于此。

输出:

To play enter [Play]: Play
Choose your class!
['Warrior', 'Mage', 'Archer']Warrior
You are: Warrior

添加以下功能

def get_input(*names):
    while True:
        try:
            for i, name in enumerate(names, 1):
                print('[{:>2}] {}'.format(i, name))
            choice = int(input()) - 1
            if choice >= 0 and choice < len(names):
                return names[choice]
        except Exception:
            print('Bad choice, try again...')

这样使用 - get_input('Warrior', 'Mage', 'Archer')

输出示例:

To play enter [Play]: Play
you were a.....
Choose your class!
[ 1] Warrior
[ 2] Mage
[ 3] Archer
4
[ 1] Warrior
[ 2] Mage
[ 3] Archer

Bad choice, try again...
[ 1] Warrior
[ 2] Mage
[ 3] Archer
dd
Bad choice, try again...
[ 1] Warrior
[ 2] Mage
[ 3] Archer
fdsg
Bad choice, try again...
[ 1] Warrior
[ 2] Mage
[ 3] Archer
1
Warrior