【发布时间】: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