Python TypeError:参数类型为';int';难道不可忍受吗?

Python TypeError:参数类型为';int';难道不可忍受吗?,python,iterator,Python,Iterator,我试图在Python2.7中编写一个hangman代码,我得到了类型错误 打印字符 对不起,我忘了添加其余的代码。这是完整的代码。这个词来自一个字典文件 import random import string WORDLIST_FILENAME = "words.txt" def load_words(): print "Loading word list from file..." # inFile: file inFile = open(WORDLIST_FIL

我试图在Python2.7中编写一个hangman代码,我得到了类型错误

打印字符

对不起,我忘了添加其余的代码。这是完整的代码。这个词来自一个字典文件

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 + "!!!")
您可以尝试:

if char in guesses: 
但是,
guesses
只是剩余猜测次数的计数,是一个整数,因此不能对其进行迭代。也许您还应该存储以前的猜测并使用:

guess_list = []
...
if char in guess_list:
...
guess_list.append(guess)
出于同样的原因,如果你走得那么远

guesses += guess

将失败-
guess
是一个字符串,
guess
是一个整数,不能相加。

依定义,Iterables是集合。
int
变量不是集合;它是单个项目。什么是
word
?它是可计算的吗?无论您迭代的是什么整数变量,都应该替换为
范围(整数值)
,而不仅仅是
整数值
。我想我们需要查看
for
循环中使用的变量的最后赋值。@RobertHarvey:“依定义,Iterables是集合”?大多数集合确实倾向于可编辑,但这两个概念是截然不同的。@NPE:我不是python专家,所以我可能是把术语弄错了。我的观点是OP需要考虑错误消息告诉他什么。