2015-05-14 58 views
-6
`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,所以我的代码知识欠缺一点

编辑 它的工作,只是把它当作书签打开,但不需要新的答案

+2

仅供参考这里,但你的数据结构(或缺乏)是想给你添麻烦。游戏是[对象]的理想用例(https://docs.python.org/3.4/tutorial/classes.html)。另外,像'20'这样的整数已经是整数。你不必用'int(20)'来施放它们。 “Rouge”是一款化妆品产品(你正在寻找“Rogue”)。 – TigerhawkT3

+0

有一件事可以真正帮助你:组织! – Zizouz212

+1

之后(并且仅在**之后**),您可以按照您的要求执行此操作,请转至http://codereview.stackexchange.com。 – leekaiinthesky

回答

0

该错误消息只有在引发异常时才会触发。例如,如果用户输入"hello",并且您致电int("hello"),则会触发ValueError并触发错误消息。

但是,如果你打电话int("5"),没有错误在这里 - 它只是返回5.然后代码进入外循环while,但它不进入内环路while(因为5是不是< = 4) 。所以它只是永远留在外层循环中。它永远不会抛出异常或离开循环。

如果需要,您可以处理Class是一个数字,但不在1和4之间的内部while循环之后。

更新:

尝试是这样的:

# Here's a method that will check whether the input is ok 
def isValidCharacterClassInput(userInput): 
    try: 
     # Return True if the input is between 1 and 4 
     # Return False if the input is an integer that doesn't fall within this range 
     return 1 <= int(userInput) and int(userInput) <= 4 
    # ValueError exception gets thrown if the input couldn't be turned into an int, for example, if the input is "2.5" or "hello" 
    except ValueError: 
     # In this case, also return False 
     return False 

# Get input from the user 
characterClassInput = input("Input 1, 2, 3 or 4: ") 

# If the input is ok, skip this part 
# If the input is not ok, enter the loop and ask for input again 
while not isValidCharacterClassInput(characterClassInput): 
    print "You have not chosen a valid number. Please try again." 
    characterClassInput = input("Input 1, 2, 3 or 4: ") 

# We don't get here until the input is validated 
print "You have chosen", characterClassInput 
+0

我没有完全理解你的解释,你能提供某种简短的代码来证明我做错了什么,以及如何解决它吗? –

+0

我添加了一些验证输入的代码。希望有所帮助! – leekaiinthesky

+0

什么是'def'?我想尽量让自己的代码保持在一个可以理解的地步,因为我是一名仍在读高中的学生。但是在我搜索的大多数问题中,我看到“def”,它有什么相关性? –