简体   繁体   English

使用 For 语句打印字符串,然后将该字符串设置为变量

[英]Using a For statement to print a string and then set that string as a variable

I am trying to make a hangman game without copying other examples.我正在尝试在不复制其他示例的情况下制作刽子手游戏。 I am fully aware I might not be using the most efficient method, but I want to see if there is a way to do this:我很清楚我可能没有使用最有效的方法,但我想看看是否有办法做到这一点:

I have a string variable called word , which is the word being guessed.我有一个名为word的字符串变量,这是被猜测的单词。 Any correctly guessed letter is placed in a list, rightguesses .任何正确猜出的字母都放在一个列表中, rightguesses

I am using a for loop to print the word, only printing the letters in the rightguesses list, and printing a _ for any letters that are not in the rightguesses list.我正在使用for循环打印单词,只打印rightguesses列表中的字母,并为不在rightguesses列表中的任何字母打印_

My problem is that I want to grab the string produced from this for loop and assign it to a variable, but I am having trouble figuring out how.我的问题是我想获取从此for循环生成的字符串并将其分配给一个变量,但我无法弄清楚如何做。 Can I use a function and return this value somehow?我可以使用 function 并以某种方式返回该值吗?

So, if the word being guessed is 'python' and rightguesses is ['p'] , the string I get is 'p _ _ _ _ _' .因此,如果被猜测的单词是 'python' 并且rightguesses['p'] ,我得到的字符串是'p _ _ _ _ _' I want to take this string and set it to a variable called userword so I can test to see if userword == word to determine if the full word has been guessed.我想获取这个字符串并将其设置为一个名为userword的变量,这样我就可以测试userword == word以确定是否已猜出完整的单词。

word = 'python'
rightguesses = []
wrongguesses = []
guesscount = 0

while guesscount < 10:
    print("Here is the current board:")
    for char in word:
        if char in rightguesses:
            print(char,end='')
        else:
            print("_",end='')
    print("\n")
    print("guess a letter")
    guess = input()
    if guess in word:
        rightguesses.append(guess)
        guesscount += 1
        print(guesscount)
    elif guess not in word and guess not in wrongguesses:
        wrongguesses.append(guess)
        print("Incorrect guess")
        guesscount += 1
    elif guess in wrongguesses or guess in rightguesses:
        print("You've already guessed that")
        continue

There's no need to do that.没有必要那样做。 In your loop that prints a letter or _ , you can tell if they guessed the whole word if you never print _ .在打印字母或_的循环中,如果您从不打印_ ,您可以判断他们是否猜到了整个单词。 So set a variable during that loop.所以在那个循环中设置一个变量。

while guesscount < 10:
    guessed_right = True
    print("Here is the current board:")
    for char in word:
        if char in rightguesses:
            print(char,end='')
        else:
            print("_",end='')
            guessed_right = False
    print("\n")
    if guessed_right:
        print("You win!")
        break
    print("guess a letter")
    guess = input()
    if guess in word:
        rightguesses.append(guess)
        guesscount += 1
        print(guesscount)
    elif guess not in word and guess not in wrongguesses:
        wrongguesses.append(guess)
        print("Incorrect guess")
        guesscount += 1
    elif guess in wrongguesses or guess in rightguesses:
        print("You've already guessed that")
        continue

Above you got an answer on an alternative way of doing this.在上面,您得到了另一种方法的答案。 But if you want to know what getting that variable might look like, here's a way to do it.但是,如果您想知道获取该变量可能是什么样子,这里有一种方法可以做到。

Where you had你在哪里

    for char in word:
        if char in rightguesses:
            print(char, end='')
        else:
            print("_", end='')
    print("\n")

you can use您可以使用

    userword = ""
    for char in word:
        if char in rightguesses:
            userword += char
        else:
            userword += "_"
    print(userword)

to, while iterating, build a word in userword and call print() only once at the end (in your example performance isn't an issue, but calling print() for every character may be a performance bottleneck in a more output-heavy program).在迭代时,在userword中构建一个单词并在最后仅调用一次print() (在您的示例中,性能不是问题,但为每个字符调用print()可能是输出更重的性能瓶颈程序)。

Of course, you can then use "_" in userword to test if it is complete or not.当然,你可以"_" in userword来测试它是否完整。

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

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