python - 寻求在 Python 中设计 While 循环的帮助

标签 python while-loop

我正在为一个类(class)项目设计一个角色扮演游戏,正在研究一个可以配置为为现有类(class)提供某些奖励的项目的噱头想法。

但是由于某种原因,让 While 循环正确循环并且不重复 print("invalid choice") 亿万年并且让函数本身正常工作,是超越我。

感谢任何帮助!!

#PowerTreadFunction
def askPowerTread():
    choice = none
    while choice not in ['A','B','C']:
        print("Invalid choice.")
    choice = input("""Your Power Treads offer the following configurations which each                give distinct bonuses, choose now:\n    
 a) DAMAGE\nb) DEFENSE\nc) ARMOR CLASS\n""")


    if choice == 'A':
        printSlow("Configuring Power Treads for Damage, you now recieve a +5 damage bonus")
        player[DAM] += 5

    elif choice == 'B':
         printSlow("Configuring Power Treads for Defense, you now recieve a +5 defense bonus")
         player[DEF] +=5

    elif choice == 'C':
         printSlow("Configuring Power Treads for Armor Class, you now recieve a +5 armor class bonus")
         player[AC] +=5

最佳答案

你的问题是缩进:

def askPowerTread():
    choice = None
    while choice not in ['A','B','C']:
        print("Invalid choice.")
    choice = input(...)

这里您正在循环打印语句,但不要求做出新的选择,这是在 while block 之外的。

def askPowerTread():
    choice = None
    while choice not in ['A','B','C']:
        print("Invalid choice.")
        choice = input(...)

应该可以解决你的问题。解决后,您粘贴的代码对我来说看起来很好。

编辑:@IanAuld,你是对的,解决这个问题:

PROMPT="""\
 Your Power Treads offer the following configurations which each
 give distinct bonuses, choose now:
  a) DAMAGE
  b) DEFENSE
  c) ARMOR CLASS

“”“

def askPowerTread():
    choice = input(PROMPT)
    while choice not in ['A','B','C']:
        print("Invalid choice.")
        choice = input(PROMPT)

关于python - 寻求在 Python 中设计 While 循环的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23596704/

相关文章:

python - Pandas DataFrame 减去横截面

python - 对齐 X 射线图像 : find rotation, 旋转和裁剪

Python sklearn 的带有分类箱的标签编码器

java - 哨兵控制循环以及如何打印多个字符串。初学者java编程

c# - 并非所有代码路径在循环时都返回值

java - java在指定范围之间连续循环

python - matplotlib stackplot 颜色 ValueError 和段错误

python - 如何在 pandas 中使用 groupby 创建索引列

Bash 使用 while read 语法附加到变量

java - while 循环开头的条件语句未被检查