为什么我在 python 中的变量不起作用
Why did my variables in python not work
我想创建一个游戏,让计算机随机选择一个词,玩家必须猜测这个词。然后计算机会告诉玩家单词中有多少个字母。然后玩家有五次机会询问单词中是否有字母。计算机只能用 "yes" 或 "no" 响应。然后,玩家必须猜出这个词。
不知何故,我在下面写的程序在让玩家猜单词之前没有准确地给玩家 5 次询问字母是否在单词中的机会。我可以知道出了什么问题吗?谢谢!
import random
WORDS = ("hello", "running", "help", "united")
word = random.choice(WORDS)
correct = word
letters=len(word)
print "There are", len(word), "letters in the word. You have 5 chances to guess the letter in the word. After which you will be required to guess the word."
guess_letter = raw_input("Guess a letter in the word.")
tries = 0
while guess_letter in word:
tries +=1
print "Yes"
guess_letter = raw_input("Guess another letter in the word.")
if tries == 4:
print "Please guess the word."
answer = raw_input("What is the word?")
if answer == correct:
print "That is correct!"
else:
print "You lose."
while guess_letter not in word:
tries +=1
print "No"
guess_letter = raw_input("Guess another letter in the word.")
if tries == 4:
print "Please guess the word."
answer = raw_input("What is the word?")
if answer == correct:
print "That is correct!"
else:
print "You lose."
两个while
循环一个接一个不是正确的逻辑(你报告的缩进也坏了,但我猜这只是你在这里复制粘贴的问题,不是你的代码本身,否则你会得到语法错误:-).
假设玩家第一次选择 不是 的 guess_letter
:然后第一个 while
立即退出 并且永远不会再输入!
你需要一个 single 循环,其迭代与 guess_letter
是否在单词中无关——只有打印的内容应该取决于它查看! for
循环可能更具可读性,但如果您愿意,您可以使用 while
来完成它——但它会是这样的:
while tries < 5:
tries += 1
guess_letter = raw_input('Guess another letter in the word.')
if guess_letter in word:
print 'yes'
else:
print 'no'
print 'Please guess the word.'
前面有初始化,后面有最后的猜测和检查。
我还看到您想以不同方式对待第一个猜测(具体来说,使用不同的提示)。这可能最好通过在提示中使用变量来实现...:[=18=]
tries = 0
art = 'a'
while tries < 5:
tries += 1
guess_letter = raw_input('Guess ' + art + ' letter in the word.')
art = 'another'
if guess_letter in word:
print 'yes'
else:
print 'no'
print 'Please guess the word.'
以下作品。它很草率,我不太喜欢它,但我认为你最大的问题可能是缩进。之后我添加了一个 while 循环来嵌套你的另外两个 while 循环。
如果你想改变一些东西来清理它,我会建议一个 while 循环(while tries < 4),然后用 if else 语句替换原来的 while 循环。
import random
WORDS = ("hello", "running", "help", "united")
word = random.choice(WORDS)
correct = word
letters=len(word)
print "There are", len(word), "letters in the word. You have 5 chances to guess the letter in the word. After which you will be required to guess the word."
guess_letter = raw_input("Guess a letter in the word.")
while tries < 4:
while guess_letter in word:
tries +=1
print "Yes"
guess_letter = raw_input("Guess another letter in the word.")
if tries == 4:
print "Please guess the word."
answer = raw_input("What is the word?")
if answer == correct:
print "That is correct!"
else:
print "You lose."
break
while guess_letter not in word:
tries +=1
print "No"
guess_letter = raw_input("Guess another letter in the word.")
if tries == 4:
print "Please guess the word."
answer = raw_input("What is the word?")
if answer == correct:
print "That is correct!"
else:
print "You lose."
break
清理版本:
import random
WORDS = ("hello", "running", "help", "united")
word = random.choice(WORDS)
correct = word
letters=len(word)
tries = 0
print "There are", len(word), "letters in the word. You have 5 chances to guess the letter in the word. After which you will be required to guess the word."
while tries < 4:
tries +=1
guess_letter = raw_input("Guess a letter in the word.")
if guess_letter in word:
print "Yes"
else:
print "No"
print "Please guess the word."
answer = raw_input("What is the word?")
if answer == correct:
print "That is correct!"
else:
print "You lose."
您的代码似乎太复杂了。我建议您使用适合您问题的 for
循环。这是您游戏的轻量级版本(在 Python 3.4 上测试):
import random
WORDS = ("hello", "running", "help", "united")
word = random.choice(WORDS)
print("There are {} letters in the word.".format(len(word)),
"You have 5 chances to guess a letter in the word.",
"After which you will be required to guess the word.")
for _ in range(5):
guess_letter = input("Guess a letter in the word: ")
if guess_letter in word:
print("YES")
else:
print("NO")
answer = input("Guess the word: ")
if answer == word:
print("That is correct!")
else:
print("Game over!")
我想创建一个游戏,让计算机随机选择一个词,玩家必须猜测这个词。然后计算机会告诉玩家单词中有多少个字母。然后玩家有五次机会询问单词中是否有字母。计算机只能用 "yes" 或 "no" 响应。然后,玩家必须猜出这个词。
不知何故,我在下面写的程序在让玩家猜单词之前没有准确地给玩家 5 次询问字母是否在单词中的机会。我可以知道出了什么问题吗?谢谢!
import random
WORDS = ("hello", "running", "help", "united")
word = random.choice(WORDS)
correct = word
letters=len(word)
print "There are", len(word), "letters in the word. You have 5 chances to guess the letter in the word. After which you will be required to guess the word."
guess_letter = raw_input("Guess a letter in the word.")
tries = 0
while guess_letter in word:
tries +=1
print "Yes"
guess_letter = raw_input("Guess another letter in the word.")
if tries == 4:
print "Please guess the word."
answer = raw_input("What is the word?")
if answer == correct:
print "That is correct!"
else:
print "You lose."
while guess_letter not in word:
tries +=1
print "No"
guess_letter = raw_input("Guess another letter in the word.")
if tries == 4:
print "Please guess the word."
answer = raw_input("What is the word?")
if answer == correct:
print "That is correct!"
else:
print "You lose."
两个while
循环一个接一个不是正确的逻辑(你报告的缩进也坏了,但我猜这只是你在这里复制粘贴的问题,不是你的代码本身,否则你会得到语法错误:-).
假设玩家第一次选择 不是 的 guess_letter
:然后第一个 while
立即退出 并且永远不会再输入!
你需要一个 single 循环,其迭代与 guess_letter
是否在单词中无关——只有打印的内容应该取决于它查看! for
循环可能更具可读性,但如果您愿意,您可以使用 while
来完成它——但它会是这样的:
while tries < 5:
tries += 1
guess_letter = raw_input('Guess another letter in the word.')
if guess_letter in word:
print 'yes'
else:
print 'no'
print 'Please guess the word.'
前面有初始化,后面有最后的猜测和检查。
我还看到您想以不同方式对待第一个猜测(具体来说,使用不同的提示)。这可能最好通过在提示中使用变量来实现...:[=18=]
tries = 0
art = 'a'
while tries < 5:
tries += 1
guess_letter = raw_input('Guess ' + art + ' letter in the word.')
art = 'another'
if guess_letter in word:
print 'yes'
else:
print 'no'
print 'Please guess the word.'
以下作品。它很草率,我不太喜欢它,但我认为你最大的问题可能是缩进。之后我添加了一个 while 循环来嵌套你的另外两个 while 循环。
如果你想改变一些东西来清理它,我会建议一个 while 循环(while tries < 4),然后用 if else 语句替换原来的 while 循环。
import random
WORDS = ("hello", "running", "help", "united")
word = random.choice(WORDS)
correct = word
letters=len(word)
print "There are", len(word), "letters in the word. You have 5 chances to guess the letter in the word. After which you will be required to guess the word."
guess_letter = raw_input("Guess a letter in the word.")
while tries < 4:
while guess_letter in word:
tries +=1
print "Yes"
guess_letter = raw_input("Guess another letter in the word.")
if tries == 4:
print "Please guess the word."
answer = raw_input("What is the word?")
if answer == correct:
print "That is correct!"
else:
print "You lose."
break
while guess_letter not in word:
tries +=1
print "No"
guess_letter = raw_input("Guess another letter in the word.")
if tries == 4:
print "Please guess the word."
answer = raw_input("What is the word?")
if answer == correct:
print "That is correct!"
else:
print "You lose."
break
清理版本:
import random
WORDS = ("hello", "running", "help", "united")
word = random.choice(WORDS)
correct = word
letters=len(word)
tries = 0
print "There are", len(word), "letters in the word. You have 5 chances to guess the letter in the word. After which you will be required to guess the word."
while tries < 4:
tries +=1
guess_letter = raw_input("Guess a letter in the word.")
if guess_letter in word:
print "Yes"
else:
print "No"
print "Please guess the word."
answer = raw_input("What is the word?")
if answer == correct:
print "That is correct!"
else:
print "You lose."
您的代码似乎太复杂了。我建议您使用适合您问题的 for
循环。这是您游戏的轻量级版本(在 Python 3.4 上测试):
import random
WORDS = ("hello", "running", "help", "united")
word = random.choice(WORDS)
print("There are {} letters in the word.".format(len(word)),
"You have 5 chances to guess a letter in the word.",
"After which you will be required to guess the word.")
for _ in range(5):
guess_letter = input("Guess a letter in the word: ")
if guess_letter in word:
print("YES")
else:
print("NO")
answer = input("Guess the word: ")
if answer == word:
print("That is correct!")
else:
print("Game over!")