第三个函数 while loop is not working(循环不工作)

The third functions while loop isnt working

提问人:omar 提问时间:8/7/2023 最后编辑:Samwiseomar 更新时间:8/7/2023 访问量:63

问:

基本上,它在第三个功能中的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:

        place=input("\nchoose to fight in either (pokemon stadium, planet namek, or the leaf village): ").lower()
        
        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")
            

    
    
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)

现在我展示了代码。当我运行它时,第三个函数给出了这个

write what pokemon you want to be (charzard, pikachu, or Gyarados) :charzard
you will be charzard
Great choice!

write what pokemon you want to face (charzard, pikachu, or Gyarados) :pikachu
charzard will face pikachu

这就是问题所在//

choose to fight in either (pokemon stadium, planet namek, or the leaf village):  
pokemon stadium
choose to fight in either (pokemon stadium, planet namek, or the leaf village):  
pokemon stadium
Not today but maybe tomorrow
Well also give you pokemon stadium music too but im in a rush

它要求无缘无故地选择两次战斗问题。我在 while 循环中和它之外都有输入,但我不知道如何让它以其他方式发挥作用(当我切断它时,我面临其他问题)

我的最后一期是//

choose to fight in either (pokemon stadium, planet namek, or the leaf village):  
s
Spell it correctly!


choose to fight in either (pokemon stadium, planet namek, or the leaf village):  
the leaf village
Spell it correctly!

当他们选择错误的战斗点(拼写错误)时,它会运行 else 语句(拼写正确),然后它会循环返回并说 Choose Again。然后,当你真正拼写正确时,它就会回到 else 语句。我不知道该怎么办?

python 函数 while-loop

评论

0赞 quamrana 8/7/2023
这是一个错别字。你有两个,但你实际上只想要一个。input()
0赞 quamrana 8/7/2023
或者,就像我对你上一个问题的评论一样,你可以将函数内的 移动到最后一行,并使其如下所示:input()a=input("choose ...

答:

0赞 Samwise 8/7/2023 #1

该函数接受一个参数,即你想战斗的地方,然后还会询问用户他们想在哪里战斗(但它忽略了结果):Where_do_u_want_to_fight

def Where_do_u_want_to_fight(a):
    
    while True:

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

请注意,这是您最初调用函数的参数,也是您刚刚向用户询问的内容。你从来没有使用过,所以无论你循环多少次并得到一个新的,你总是在检查原来的。aplaceplaceplacea

若要解决此问题,请删除该参数。由于您已经在函数内部请求输入,因此它是多余的:

def Where_do_u_want_to_fight():  # no argument
    
    while True:

        place=input("\nchoose to fight in either (pokemon stadium, planet namek, or the leaf village): ").lower()
        
        if place == "pokemon stadium":  # check 'place', not 'a'

调用它时,不带参数地调用它:

Where_do_u_want_to_fight()  # no 'place' argument needed here

评论

0赞 quamrana 8/7/2023
但是,如果你这样做,它使该函数与编写的其他函数不同。
0赞 Samwise 8/7/2023
是的;OP 最好让 IMO 了解这种差异,因为它可能会让他们对范围界定:)有一些见解
-2赞 Khaled Koubaa 8/7/2023 #2

看起来您在函数中遇到了几个问题。让我们一步一步地解决这些问题。Where_do_u_want_to_fight

  1. 重复输入问题:“选择战斗”问题被问两次的原因是,你在循环内部和外部都有输入提示。要解决此问题,您应该只在循环中设置输入提示。while

    下面是输入提示符代码的更正部分:

    def Where_do_u_want_to_fight(a):
        while True:
            place = input("\nChoose 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 I'm 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 I'm 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 I'm in a rush")
                break
            else:
                print("Choose a valid location!")
    
  2. 拼写错误处理:要处理拼写不正确的问题,您需要更改要检查的变量。您当前正在检查(您要面对的角色)是否等于某个位置(例如,)。相反,您应该检查(选择的战斗位置)是否等于正确的位置字符串。a"pokemon stadium"place

    我已经在上面提供的更新的函数代码中更正了这一点。

通过这些更正,您的函数应该可以按预期工作。它会不断询问一个有效的战斗地点,直到用户输入正确的选择,然后它会跳出循环并相应地进行。Where_do_u_want_to_fight