【问题标题】:Hangman Game not Printing Out Correct Letters Guessed刽子手游戏没有打印出猜测的正确字母
【发布时间】:2020-10-15 08:33:49
【问题描述】:

新程序员在这里制作他们的第一个游戏——刽子手! 基本上,我有一个随机词的全局列表(我稍后会添加更多),一个打印出随机词(用于测试)的全局调用,一种用户一次猜测一个字母的方法。在用户输入法中,单词也会打印出来,用星号代替不正确的字母。

我目前处于用户输入字母的部分,并且正在检查它是否在计算机的单词中。我收到此错误消息:

ValueError: not enough values to unpack (expected 2, got 1)

这是我的主要代码-有点乱,但请给我任何指示!

import random
'''
hangman components:

-dictionary of words
-computer chooses a word from the wordlist
-the word has to be converted into an array or looped through or checking index
-does the user want to guess a letter?
    if the letter is in the word, show it/print it out
    

'''
#simple hangman game with a preloaded list of words!

class hangmanGame():
    
    global wordList 
    wordList = ['cat','dog','apple','pineapple']
    global computer_choice
    computer_choice = random.choice(wordList)
    
    def computer(self):
        #getter method which returns what choice the computer made for the word
        print("The word is %s" % computer_choice)
    
    def user_input(self):
        #method where the user enters a letter, and it is checked whether it is in the word or not. if it is- remove the * and keep going
        #you get 10 turns!
        
        for letter in computer_choice:
            x = print("*")
        
        turns = 0
        letter_choice = input("enter in a letter to check if it is in the word:").lower( )
        turns = turns+1
        #check if the chosen letter is in the computer's word
        # if it is, then print out asterisks except for the correct letter
        for (i,letters) in computer_choice:
            if letter_choice == computer_choice(i):
                print(letters)
            else:
                print("*")

我一直在 Jupyter 笔记本中运行它,这是我一直在使用的调用的第二个单元格:

hmg =  hangmanGame()
print(hmg.computer())
print(hmg.user_input())

目前打印出来的是:


The word is dog #randomly generated
None
*
*
*
enter in a letter to check if it is in the word:d

我不太清楚为什么会出现“None”——如果你这样做了,请告诉我!其余的错误是这样说的:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-f471a116dcad> in <module>
      1 hmg =  hangmanGame()
      2 print(hmg.computer())
----> 3 print(hmg.user_input())

<ipython-input-1-6ce94e2f00a5> in user_input(self)
     36         #check if the chosen letter is in the computer's word
     37         # if it is, then print out asterisks except for the correct letter
---> 38         for (i,letters) in computer_choice:
     39             if letter_choice == computer_choice(i):
     40                 print(letters)

ValueError: not enough values to unpack (expected 2, got 1)

感谢您的帮助!

【问题讨论】:

    标签: python jupyter-notebook jupyter valueerror


    【解决方案1】:

    我将只关注您关于为什么会出现 None 这个词的问题。

    TLDR;只需在您的computer() 方法中将print 替换为return。下面解释

    你已经在你的类中创建了一个名为computer的方法。 你已经定义了它:

    def computer(self):
            #getter method which returns what choice the computer made for the word
            print("The word is %s" % computer_choice)
    

    问题是没有返回语句。由于您使用的是 Jupyter Notebooks,因此笔记本单元格将返回已运行的函数的值。你的函数(或者在这种情况下是类方法),没有返回值。

    尝试将 print() 语句更改为 return 语句。即

    def computer(self):
            #getter method which returns what choice the computer made for the word
            return "The word is %s" % computer_choice
    

    【讨论】:

    • 我应该补充一点,如果您只是将原始代码作为 python 文件运行,则“无”不会出现。只是因为你用的是笔记本
    【解决方案2】:

    问题是您在第 38 行的 for 循环中为列表 computer_choice 提供了两个索引。您想通过在其中添加 i 值来调用什么?我假设您正在尝试检查输入字母 letter_choice 是否与随机选择的单词中的任何字母匹配,因为无论如何您都不需要两个变量,只需尝试

    
    for letters in computer_choice:
                if letter_choice == computer_choice(letters):
                    print(letters)
    
    

    应该打印正确的猜测字母!

    【讨论】:

    • 此外,如果您的列表是二维的,您将使用两个索引,在这种情况下,您会拨打电话,例如 your_list[i, j],它将从索引为 i 的列中调用项目,以及索引为 j 的行。
    猜你喜欢
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多