简体   繁体   English

if语句在函数内部的行为不同

[英]if statement behaving differently inside function

Alright, so I need to make a simple hangman game for python. 好吧,所以我需要为python创建一个简单的子手游戏。 and I'm having problems with my function and variables. 而且我的函数和变量有问题。 Guessing and replacing "blanks" works when I try a simple if loop, but once I put everything into a proper function it gives me issues. 当我尝试一个简单的if循环时,猜测并替换“空白”是可行的,但是一旦我将所有内容放入适当的函数中,它就会给我带来问题。

For example, when a word has more than 1 instance of a letter it shows both while trying the if loop outside the function, but using the function it only shows 1. I deleted my old program and restarted anew, but I'm still having trouble with this function (which is extra maddening). 例如,当一个单词有一个以上的字母实例时,尝试在函数外执行if循环时会同时显示两个字母,但是使用该函数时它只会显示1。我删除了旧程序并重新启动,但是我仍然有此功能的麻烦(这很令人发疯)。 Before I was getting issues with it updating starCopy and attempts as well. 在我遇到问题之前,还更新了starCopy和尝试。 Having this much trouble with such a simple program is kinda bumming me out. 这么简单的程序有这么多麻烦,真让我不寒而栗。 :\\ :\\

Here's the loop that works while outside the function: 这是在函数外部起作用的循环:

import random
#declare variables
guess="a"
choice="barnyard"
starredUp=len(choice) * "*"
starCopy=starredUp
attemptedLetters=[]
running=True
attempts=0

if guess in choice:
    starCopy=list(starCopy)
    for i, x in enumerate(choice):
        if x == guess:
            starCopy[i]=guess
            print(" ".join(starCopy));
            print("Letter in word! " + "Wrong Attempts: " + str(attempts))

Returns * * r * * * r * Letter in word! Wrong Attempts: 0 返回* * r * * * r * Letter in word! Wrong Attempts: 0 * * r * * * r * Letter in word! Wrong Attempts: 0

Now, when I try calling the function: 现在,当我尝试调用该函数时:

def guessMan(guess, choice, attempts, starCopy):
    if guess in choice:
        starCopy=list(starCopy)
        for i, x in enumerate(choice):
            if x == guess:
                starCopy[i]=guess
                print(" ".join(starCopy))
                print("Letter in word! " + "Wrong Attempts: " + str(attempts))
                return(starCopy)
    elif guess not in choice:
          attempts+=1
          print("yo fix it")
    elif guess in attemptedLetters:
          print("already guessed")
guessMan("r", choice, attempts, starCopy)

it returns: 它返回:

* * r * * * * *
Letter in word! Wrong Attempts: 0

Honestly not sure why I'm hitting such a brick wall with this. 老实说,我不确定为什么要用这种方式撞到这样的砖墙。 Feel like I'm missing something super simple. 感觉就像我缺少了一些超级简单的东西。

The output changes because in your function-based example, you return the variable starCopy. 输出更改,因为在基于函数的示例中,您返回了变量starCopy。 It returns immediately after it hits the first letter that matches "guess", so only the first letter gets replaced. 它在碰到与“ guess”匹配的第一个字母后立即返回,因此只有第一个字母被替换。 Moving the return command to the end should work: 将return命令移到末尾应该可以工作:

def guessMan(guess, choice, attempts, starCopy):
    if guess in choice:
        starCopy=list(starCopy)
        for i, x in enumerate(choice):
            if x == guess:
                starCopy[i]=guess
                print(" ".join(starCopy))
                print("Letter in word! " + "Wrong Attempts: " + str(attempts))

    elif guess not in choice:
          attempts+=1
          print("yo fix it")
    elif guess in attemptedLetters:
          print("already guessed")

    return(starCopy)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM