You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pokemon游戏中第三个函数的while循环异常问题求助

Pokemon游戏战斗地点选择函数循环异常问题解决

问题描述

我开发的Pokemon游戏中,第三个函数Where_do_u_want_to_fight的while循环存在异常。预期逻辑是:接收用户输入,若输入匹配指定选项则执行对应分支并退出循环;若输入错误则提示拼写错误并重新输入。但实际运行时,即使输入匹配选项,函数仍提示拼写错误,无法正常执行对应分支。

错误代码

def who_do_you_want_to_be(x):
    print("you will be "+x)
    print("Great choice!")
    
def who_do_you_want_to_face(a, x):
    print(a + " will face "+x + "\n" )
    
def Where_do_u_want_to_fight(a):
    
    while True:
  
        if a=="pokemon stadium":
            print("Not today but maybe tomorrow")
            print("Well also give you pokemon stadium music too but im in a rush")
            break
            
        elif a=="planet namek":
            print("Not today but maybe tomorrow")
            print("Well also give you planet namek music too but im in a rush")
            break
            
        elif a=="the leaf village":
            print("Not today but maybe tomorrow")
            print("Well also give you the leaf village music too but im in a rush")
            break
            
        else:
            print("Spell it correctly!\n")
            place=input("choose to fight in either (pokemon stadium, planet namek, or the leaf village): ").lower()
    
character=input("write what pokemon you want to be (charzard, pikachu, or Gyarados) :").lower() 
who_do_you_want_to_be(character)

choice=input("\nwrite what pokemon you want to face (charzard, pikachu, or Gyarados) :").lower()    
who_do_you_want_to_face(character, choice)

place=input("\nchoose to fight in either (pokemon stadium, planet namek, or the leaf village): ").lower()
Where_do_u_want_to_fight(place)

问题根源

  1. 循环内未更新判断变量:函数中的while True循环只会在一开始检查传入的参数a,如果第一次输入错误,后续重新输入的place没有赋值给a,导致循环永远在判断最初的错误值,无法匹配正确选项。
  2. 逻辑顺序错误:初始输入的判断放在循环开头,但后续重新输入的内容完全没参与循环的判断逻辑,相当于白输入。

修复后的代码

def who_do_you_want_to_be(x):
    print("you will be "+x)
    print("Great choice!")
    
def who_do_you_want_to_face(a, x):
    print(a + " will face "+x + "\n" )
    
def Where_do_u_want_to_fight():
    # 把输入逻辑移到循环内部,每次循环都获取新输入并判断
    while True:
        place = input("choose to fight in either (pokemon stadium, planet namek, or the leaf village): ").lower()
        if place == "pokemon stadium":
            print("Not today but maybe tomorrow")
            print("We'll also give you pokemon stadium music too but im in a rush")
            break
        elif place == "planet namek":
            print("Not today but maybe tomorrow")
            print("We'll also give you planet namek music too but im in a rush")
            break
        elif place == "the leaf village":
            print("Not today but maybe tomorrow")
            print("We'll also give you the leaf village music too but im in a rush")
            break
        else:
            print("Spell it correctly!\n")
    
character=input("write what pokemon you want to be (charzard, pikachu, or Gyarados) :").lower() 
who_do_you_want_to_be(character)

choice=input("\nwrite what pokemon you want to face (charzard, pikachu, or Gyarados) :").lower()    
who_do_you_want_to_face(character, choice)

# 直接调用函数,无需传入外部参数
Where_do_u_want_to_fight()

修复说明

  • 将输入逻辑整合到函数内部的循环中,每次循环都会获取新的用户输入并直接进行判断,确保输入值和判断逻辑同步。
  • 移除了函数的参数a,避免外部初始输入值干扰循环内的判断流程。
  • 顺带修正了原代码中的拼写错误Well→We'll,让文本更规范。

内容的提问来源于stack exchange,提问作者omar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 06:15:55