我试图在python 2.7中做一个刽子手代码,我得到的是 type error
打印字符 .
对不起,我忘了添加剩下的代码了 . 这是完整的代码 . 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 + "!!!")
1 回答
你试试:
但是,
guesses
只是剩下的猜测数量的一个整数,所以你不能迭代它 . 也许您还应该存储以前的猜测并使用:出于同样的原因,如果你走得那么远
会失败 -
guess
是一个字符串而guesses
是一个整数,不能一起添加 .