我制作的 Hangman 程序遇到问题

发布于 2024-12-15 17:04:26 字数 2010 浏览 2 评论 0原文

我无法从代码中获得正确的输出。我需要它一一提供头部、身体、手臂、腿,但它不会。请检查我的代码。

import random
max_wrong = len(HANGMAN) -1
WORDS = ("Caleb","Owen","Ben","Adriane","Marley")
word = random.choice(WORDS)
so_far = "-" * len(word)
wrong = 0
used = []

while wrong < max_wrong and so_far != word:  #The start of the sequence  to make it loop
    print(HANGMAN[wrong])
    print("you used the following letters: ",used)
    print("So far the length of the word is ",so_far)

    guess = raw_input("What letter do you think is in the word? ")
    guess = guess.upper()

    while guess in used:
        print("Hey,wait a minute you have already guessed that word.")
        guess = raw_input("What letter do you think is in the word? ")
        guess = guess.upper()

used.append(guess)

if guess in used:
    print('Yes',guess,"is in the word")
    # new so_far
    new = " "
    for i in range(len(word)):
        if guess == word[i]:
            new += guess 

    else:
    new += so_far[i]
    max_wrong = len(HANGMAN) -1

    so_far = new

else:
    print("Sorry ", guess, " is not in the word")
    wrong += 1 

if wrong == max_wrong:
    print(HANGMAN[wrong])
    print"Oops sorry you have been hanged!"

else:
    print("You guessed it the word was ",word)

顺便说一句,我已经创建了带有“图形”的元组,但堆栈溢出不允许我显示它,所以我把它留在了后面。它不会提供正文,并且一直说该字母在单词中。 输出是这样的,即使它不在单词中

 ------
 |    |
 |
 |
 |
 |
 |
 |
 |
----------

('you used the following letters: ', [])
('So far the length of the word is ', '-----')
What letter do you think is in the word? n
('Yes', 'N', 'is in the word')

 ------
 |    |
 |
 |
 |
 |
 |
 |
 |
 ----------

('you used the following letters: ', ['N'])
('So far the length of the word is ', ' -----')
What letter do you think is in the word? k
('Yes', 'K', 'is in the word')

  ------
  |    |
  |
  |
  |
  |
  |
  |
  |
  ----------

 ('you used the following letters: ', ['N', 'K'])
 ('So far the length of the word is ', '  ----')
 What letter do you think is in the word? 

I'm having trouble getting the correct output out of my code. I need it to provide the head, body, arms, legs one by one, but it won't. Please check my code.

import random
max_wrong = len(HANGMAN) -1
WORDS = ("Caleb","Owen","Ben","Adriane","Marley")
word = random.choice(WORDS)
so_far = "-" * len(word)
wrong = 0
used = []

while wrong < max_wrong and so_far != word:  #The start of the sequence  to make it loop
    print(HANGMAN[wrong])
    print("you used the following letters: ",used)
    print("So far the length of the word is ",so_far)

    guess = raw_input("What letter do you think is in the word? ")
    guess = guess.upper()

    while guess in used:
        print("Hey,wait a minute you have already guessed that word.")
        guess = raw_input("What letter do you think is in the word? ")
        guess = guess.upper()

used.append(guess)

if guess in used:
    print('Yes',guess,"is in the word")
    # new so_far
    new = " "
    for i in range(len(word)):
        if guess == word[i]:
            new += guess 

    else:
    new += so_far[i]
    max_wrong = len(HANGMAN) -1

    so_far = new

else:
    print("Sorry ", guess, " is not in the word")
    wrong += 1 

if wrong == max_wrong:
    print(HANGMAN[wrong])
    print"Oops sorry you have been hanged!"

else:
    print("You guessed it the word was ",word)

By the way I have already created the tuple with the "graphics" but stack overflow would not let me show it so I left it behind. It won't provide the body and it keeps saying that the letter is in the word.
The output is this even though it is not in the word

 ------
 |    |
 |
 |
 |
 |
 |
 |
 |
----------

('you used the following letters: ', [])
('So far the length of the word is ', '-----')
What letter do you think is in the word? n
('Yes', 'N', 'is in the word')

 ------
 |    |
 |
 |
 |
 |
 |
 |
 |
 ----------

('you used the following letters: ', ['N'])
('So far the length of the word is ', ' -----')
What letter do you think is in the word? k
('Yes', 'K', 'is in the word')

  ------
  |    |
  |
  |
  |
  |
  |
  |
  |
  ----------

 ('you used the following letters: ', ['N', 'K'])
 ('So far the length of the word is ', '  ----')
 What letter do you think is in the word? 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人间☆小暴躁 2024-12-22 17:04:26

好吧,对于初学者来说,看起来你有一个未定义的变量:

max_wrong = len(HANGMAN) -1

你的意思是

max_wrong = len(word) -1

?在这种情况下,您必须在定义单词之后定义它。

您还必须看看如何设置循环...例如,您有一个缩进的“else”(不确定这是否只是通过复制和粘贴)。如果您尝试

new = " "

设置一个空白字符串,并在每次正确猜测字母时添加字母,您可能希望将其放在 if 循环之外,否则每次运行循环时都会覆盖一个新的空白字符串。

这是你的课堂作业吗?

Well, for starters, it looks like you have an undefined variable:

max_wrong = len(HANGMAN) -1

Do you mean that to be

max_wrong = len(word) -1

?? In that case you'd have to define it after you defined word.

You've also got to look at how you're setting up your loops... for example you've got an indented "else" (not sure if this was just through your copy & paste). And if you're trying to use

new = " "

to set up a blank string and add in letters each time they are correctly guessed you probably want that outside your if loop, otherwise it will overwrite a new blank string each time it runs through the loop.

Is this for your class homework?

葬﹪忆之殇 2024-12-22 17:04:26

首先:

if guess in used: 
    print('Yes',guess,"is in the word")

您不想检查一下猜测是否在单词中吗?

Start with:

if guess in used: 
    print('Yes',guess,"is in the word")

Don't you want to check if guess is in word?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文