`import time
WarriorSpellOne, WarriorSpellTwo, WarriorSpellThree, WarriorSpellUltimite = ("Slash"), ("Hammer Down"), ("Flame Strike"), ("Ragnarok")
MageSpellOne, MageSpellTwo, MageSpellThree, MageSpellUltimite = ("Fireball"), ("Lightning Strike"), ("Necromancy"), ("Mutation")
ArcherSpellOne, ArcherSpellTwo, ArcherSpellThree, ArcherSpellUltimite = ("Tri-Shot"), ("Aimed Shot"), ("Snare"), ("Arrow Rain")
RougeSpellOne, RougeSpellTwo, RougeSpellThree, RougeSpellUltimite = ("Backstab"), ("Smoke Bomb"), ("Blade Toss"), ("Shadow Wars")
SpellOne, SpellTwo, SpellThree, SpellUltimite = ("N/A"), ("N/A"), ("N/A"), ("N/A")
WarriorHealth, WarriorAttack, WarriorMana = int(200), int(10), int(100)
MageHealth, MageAttack, MageMana = int(75), int(10), int(200)
ArcherHealth, ArcherAttack, ArcherMana = int(150), int(15), int(150)
RougeHealth, RougeAttack, RougeMana = int(100), int(20), int(50)
ClassHealth, ClassAttack, ClassMana = int(0), int(0), int(0)
ClassSelected = ("N/A")
Class = int(0)
Confirm = int(0)
try:
Class = int(input("\nSelect a class, Warrior(1), Mage(2), Archer(3), Rouge(4)"))
while Confirm != 1:
while Class <= 4 and Class >= 1 :
if Class == 1:
ClassHealth, ClassAttack, ClassMana = WarriorHealth, WarriorAttack, WarriorMana
SpellOne, SpellTwo, SpellThree, SpellUltimite = WarriorSpellOne, WarriorSpellTwo, WarriorSpellThree, WarriorSpellUltimite
ClassSelected = ("Warrior")
if Class == 2:
ClassHealth, ClassAttack, ClassMana = MageHealth, MageAttack, MageMana
SpellOne, SpellTwo, SpellThree, SpellUltimite = MageSpellOne, MageSpellTwo, MageSpellThree, MageSpellUltimite
ClassSelected = ("Mage")
if Class == 3:
ClassHealth, ClassAttack, ClassMana = ArcherHealth, ArcherAttack, ArcherMana
SpellOne, SpellTwo, SpellThree, SpellUltimite = ArcherSpellOne, ArcherSpellTwo, ArcherSpellThree
ClassSelected = ("Archer")
if Class == 4:
ClassHealth, ClassAttack, ClassMana = RougeHealth, RougeAttack, RougeMana
SpellOne, SpellTwo, SpellThree, SpellUltimite = RougeSpellOne, RougeSpellTwo, RougeSpellThree, RougeSpellUltimite
ClassSelected = ("Rouge")
print ("\nYou have selected the {} class. {} Health, {} Attack, {} Mana".format(ClassSelected, ClassHealth, ClassAttack, ClassMana))
print ("\nYour spells are; {}, {}, {} and {}".format(SpellOne, SpellTwo, SpellThree, SpellUltimite))
time.sleep(3)
Confirm = int(input("\nDo you want to continue with this class? Yes(1), No(0)"))
if Confirm == 0:
ClassHealth, ClassAttack, ClassMana, SpellOne, SpellTwo, SpellThree, SpellUltimite = int(0), int(0), int(0), ("N/A"), ("N/A"), ("N/A"), ("N/A")
except (ValueError, TypeError):
Class = int(input("\nInvalid Class, try again | Warrior(1), Mage(2), Archer(3), Rouge(4)"))`
我正在努力让自己的游戏变得有趣,这是课堂选择阶段 验证不起作用,使用IDLE时,如果数字超出范围,例如5输入,没有任何反应。当我输入一个字符时,它会说错误短语,但是当我重新输入相同的字符时,它会使程序崩溃。
有任何改善验证的建议吗?我也是python的新手,所以我的代码知识缺乏一点
修改 处理它,只是将它作为书签打开,但不需要新的答案
答案 0 :(得分:0)
仅在抛出异常时才会触发错误消息。例如,如果用户输入"hello"
,并且您调用int("hello")
,则会抛出ValueError
并触发错误消息。
但是,如果你调用int("5")
,这里没有错误 - 它只返回5.然后代码进入外部while
循环,但它不会进入内部{{1} } loop(因为5不是&lt; = 4)。所以它只是永远位于外循环中。它永远不会抛出异常或离开循环。
如果需要,您可以处理while
是一个数字但不在内部Class
循环之后的1到4之间的情况。
<强>更新强>
尝试这样的事情:
while