![](/img/trans.png)
[英]Hangman Game - IndexError: list index out of range - It's because of my for loop on line 89 but I don't know how to fix it
[英]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
你可以這樣做:
你定義了一個方法 Classes ,它正在做一些奇怪的事情,我不知道你的意圖是什么,但你應該改變:
def Classes(Warrior, Mage, Archer):
Classes = (Warrior, Mage, Archer)
變成這樣:
classes = ["Warrior", "Mage", "Archer"]
包含所有可用類的列表。
之后,您可以讓玩家像這樣選擇他的職業:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.