简体   繁体   English

python中的猜词游戏

[英]word guessing game in python

I'm trying to create a guessing game that prompts the user for a word, then prompts for letter guesses.我正在尝试创建一个猜谜游戏,提示用户输入单词,然后提示猜字母。 I was having trouble with my code - my expected output was " guess 'not found'" if the letter guess was not in the word, but no matter the guess, the output continues to be " guess 'found'".我的代码有问题 - 如果单词中没有字母guess,我的预期输出是“guess 'not found'”,但无论猜测如何,输出仍然是“guess 'found'”。 Here's my original code:这是我的原始代码:

word = input('Enter a guess word: ')
while True:
        guess = input('Enter a guess letter: ')
        for word in guess:
                if guess in word:
                        print('\t>', guess, 'found')
                else:
                        print('\t>', guess, 'not found')

for example if the user inputs 'hello' as the word and 'p' as the guess, it will still print '> p found'.例如,如果用户输入 'hello' 作为单词并输入 'p' 作为猜测,它仍然会打印 '> p found'。 Later, I was just fooling around and it worked?后来,我只是在胡闹,它奏效了吗? Firstly, when I tried to apply this to my original code, it didn't work (the problem remained, just that the output continuously returned 'not found' instead).首先,当我尝试将其应用于我的原始代码时,它不起作用(问题仍然存在,只是输出不断返回“未找到”)。 Secondly, I do not understand how this ended up working... This is the working(?) code:其次,我不明白这最终是如何工作的......这是工作(?)代码:

for x in 'word':
        pass
for word in x:
        if 'p' in word:
                print(':)')
        else:
                print(':(')

attempt to apply to original code:尝试应用于原始代码:

word = input('Enter a guess word: ')
while True:
        guess = input('Enter a guess letter: ')
        for x in word:
                pass
        for word in x:
                if guess in word:
                        print('\t>', guess, 'found')
                else:
                        print('\t>', guess, 'not found')

You are overwriting the value of the word variable with the following line:您正在使用以下行覆盖 word 变量的值:

 for word in guess:

change the variable name更改变量名

You have to be careful when naming your variables.命名变量时必须小心。 You first declare word as your target but then use word again to represent each character of guess .您首先将word声明为目标,然后再次使用word来表示guess每个字符。

Let's say my guess word is test and my guess is a .比方说,我的猜测词是测试我的猜测

Since you wrote for word in guess , word is now a .由于您for word in guess写作,因此word现在是 a That is why if word in guess will always return True , because a is indeed in a .这就是为什么if word in guess总是返回True ,因为a确实在a 中

Here is how to fix it:以下是修复方法:

word = input('Enter a guess word: ')
while True:
        guess = input('Enter a guess letter: ')
        for x in guess: # or any other variable name besides "word"
                if x in word:
                        print('\t>', guess, 'found')
                else:
                        print('\t>', guess, 'not found')

You are basically over writing the variable word with the letter when you iterate over the loop.当您迭代循环时,您基本上是用字母写了变量word Also, you need to iterate over word and not guess :此外,您需要遍历word而不是guess

word = input('Enter a guess word: ')
while True:
        guess = input('Enter a guess letter: ')
        for words in word:
                
                if guess==words:
                        print('\t>', guess, 'found')
                        break
                else:
                        print('\t>', guess, 'not found')

In your code, you first defined word as a variable and then stored the input in it.在您的代码中,您首先将word定义为变量,然后将输入存储在其中。

word = input('Enter a guess word: ')

Then, you tried to execute the while loop there, you're taking the input letter in guess :然后,您尝试while loop那里执行while loop ,您将输入的字母输入guess

guess = input('Enter a guess letter: ')
    

The problem begins from the next line, inside this loop:问题从下一行开始,在这个循环内:

for word in guess:
        if guess in word:
                print('\t>', guess, 'found')
        else:
                print('\t>', guess, 'not found')

In the loop, the iteration condition is for word in guess , which considers word as a local item inside the loop containing each element of the guess per iteration.在循环中,迭代条件是for word in guess ,它将word视为包含每次迭代的guess每个元素的循环内的局部项。

In next line, the if condition checks if guess in word , which means, you're checking if the guess is in the word , but this word is nothing but the word introduced in for loop .在下一行中,if 条件检查if guess in word ,这意味着,您正在检查guess是否在word ,但这个word只不过是for loop引入的word So, technically you've overwritten the actual word introduced at the beginning, with the letters from the guess input.因此,从技术上讲,您已经用猜测输入中的字母覆盖了actual word引入的actual word

As the guess from input and the word in guess are same, this condition is satisfied for the letter p .由于输入的guessguess中的word相同,因此字母p满足此条件。 Hence you're getting the p found as the result.因此,您将找到p found作为结果。

But it should be changed too, take each letter from guess , and check whether the letter is in word .但它也应该改变,从guess取出每个letter ,并检查letter是否在word Just change the name of the variable word in the condition for word in guess to something like for letter in guess .只需将for word in guess的条件中变量word的名称更改for word in guess类似于for letter in guess Also change if guess in word to if letter in word , while you're running a loop for letter in guess .还要将if guess in word更改为if letter in word ,同时for letter in guess运行循环。 And it'll work properly:它会正常工作:

word = input('Enter a guess word: ')
while True:
    guess = input('Enter a guess letter: ')
    for letter in guess:
            if letter in word:
                    print('\t>', guess, 'found')
            else:
                    print('\t>', guess, 'not found')

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

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