[英]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
.由于输入的
guess
和guess
中的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.