我正在尝试在python 2.7中执行一个hangman代码,并且我在
的行周围得到类型错误print char。
对不起,我忘了添加剩下的代码了。这是完整的代码。 Word来自字典文件。
import random
import string
WORDLIST_FILENAME = "words.txt"
def load_words():
print "Loading word list from file..."
# inFile: file
inFile = open(WORDLIST_FILENAME, 'r', 0)
# line: string
line = inFile.readline()
# wordlist: list of strings
wordlist = string.split(line)
print " ", len(wordlist), "words loaded."
return wordlist
def choose_word(wordlist):
return random.choice(wordlist)
wordlist = load_words()
print "Welcome to Hangman where your wits will be tested!"
name = raw_input("Input your name: ")
print ("Alright, " + name + ", allow me to put you in your place.")
word = random.choice(wordlist)
print ("My word has ")
print len(word)
print ("letters in it.")
guesses = 10
failed = 0
for char in word:
if char in guesses:
print char,
else:
print "_",
failed += 1
if failed == 0:
print "You've Won. Good job!"
break
#
guess = raw_input("Alright," + name + ", hit me with your best guess.")
guesses += guess
if guess not in word:
guesses -= 1
print ("Wrong! I'm doubting your intelligence here," + name)
print ("Now, there's only " + guesses + " guesses left until the game ends.")
if guesses == 0:
print ("I win! I win! I hanged " + name + "!!!")
答案 0 :(得分:1)
你试试:
if char in guesses:
但是,guesses
只是剩下的猜测数量的 count ,一个整数,所以你不能迭代它。也许您还应该存储以前的猜测并使用:
guess_list = []
...
if char in guess_list:
...
guess_list.append(guess)
出于同样的原因,如果你到目前为止
guesses += guess
会失败 - guess
是一个字符串而guesses
是一个整数,不能一起添加。