2015-03-13 92 views
-1

我想创建一个游戏,在这个游戏中电脑选择一个随机单词,玩家必须猜测这个单词。然后计算机会告诉玩家这个词有多少个字母。然后玩家有五次机会问一个字是否在这个单词中。电脑只能回答“是”或“否”。然后,玩家必须猜测这个词。为什么我的变量在python中不起作用

不知何故,下面我写的程序并没有准确地给玩家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." 
+3

请解决您的缩进 – MattDMo 2015-03-13 16:31:35

+1

如果你只是你为什么不使用'为loop'而是有5种变化? – 2015-03-13 16:34:54

回答

1

两个while环一前一后是不正确的逻辑(缩进你举报也断了,但我猜这只是一个你的问题,复制和粘贴在这里,不是你代码本身,否则你会得到语法错误:-)。

假设例如在第一时间玩家挑选一个guess_letter不在字:那么第一个while立即退出,决不会再次进入

您需要一个单个循环,其迭代与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.' 

和之后检查。

我也看到你想对待不同的第一个猜测(具体来说,使用不同的提示)。这可能是通过在提示符中使用变量最好地实现的:...

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.' 
+0

谢谢你ALEX MARTELLI! – 2015-03-16 03:26:04

+0

@ G.F.Yu,欢迎您 - 请记住**接受**答案(左边的复选标记形状轮廓上的clic),至关重要的StackOverflow礼节! - ) – 2015-03-16 03:46:23

0

以下工作。它的马虎,我不喜欢它,但我认为你最大的问题可能是缩进。之后,我添加了一个while循环来嵌套你的两个while循环。

如果你想改变一些东西来清理它,我会建议一个while循环(while try < 4),然后用if语句替换原来的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." 
+0

谢谢你WOODSY! – 2015-03-16 03:25:53

0

你的代码看起来太复杂了。我建议你使用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!") 
+0

谢谢你ALEXANDRE FIGURA! – 2015-03-16 03:25:44