【问题标题】:Double Validation Error, How to fix?双重验证错误,如何解决?
【发布时间】:2015-07-25 12:50:15
【问题描述】:
`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新手,所以我对代码的了解有点不足

编辑 正在处理它,只是将它作为书签打开,但不需要新的答案

【问题讨论】:

  • 这里仅供参考,但您的数据结构(或缺少数据结构)只会给您带来麻烦。游戏是objects 的理想用例。此外,像20 这样的整数已经是整数。您不必使用int(20) 投射它们。而Rouge 是化妆品(你在找Rogue)。
  • 一件事可以真正帮助您:组织!
  • 在(并且仅在之后)你得到它做你想做的事情之后,请把它交给codereview.stackexchange.com
  • @leekaiinthesky 请问为什么?我可能会通过更改其初始目的来对该代码采用不同的方法,但是将其带到代码审查网站的目的是什么? (友好的问题,没有敌意:D)
  • 代码审查堆栈交换是供人们发布工作代码并让社区就如何使其变得更好提出建议(更符合一般的编程最佳实践)。在我之前发表评论的人建议您需要更好的组织和自定义数据结构。这些建议不会直接解决您在此处发布的问题,甚至可能不会改变您的代码的运行方式。但是,它们将使您的代码更好、更易于阅读和更易于编写,而它们正是代码审查社区旨在解决的问题。

标签: python


【解决方案1】:

只有在抛出异常时才会触发错误消息。例如,如果用户输入"hello",而您调用int("hello"),则会抛出ValueError 并触发错误消息。

但是,如果你调用int("5"),这里没有错误——它只是返回5。然后代码进入外部while循环,但它没有进入内部while循环(因为5是不

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

更新:

试试这样的:

# 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

【讨论】:

  • 我没有完全理解你的解释,你能否提供一些简短的代码来证明我做错了什么以及如何解决它?
  • 我添加了一些代码来验证输入。希望有帮助!
  • 什么是'def'?我想尝试将我的代码保持在我可以理解的程度,因为我还是一名还在上高中的学生。但是我在搜索的大多数问题中都看到了“def”,它有什么相关性?
  • def 是如何在 Python 中定义函数。定义自己的函数、方法和对象类是任何编程语言中最强大的部分之一。您可以在此处阅读有关如何使用 def 定义 Python 函数的信息:docs.python.org/2/tutorial/controlflow.html#defining-functions。事实上,您可以在这里阅读整个教程:docs.python.org/2/tutorial/index.html。 (我已将您链接到 Python 2 教程。如果您使用的是 Python 3,那么也有 Python 3 教程,但在这个阶段,您使用的 Python 版本并不重要。)跨度>
  • 我强烈建议您完成本教程。如果你真的刚刚开始,Stack Overflow 的帮助不如先学习基础知识。祝你好运,欢迎来到计算机编程的美妙世界!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多