类型为'int'的TypeError参数不可迭代,'int'对象没有属性'_getitem_'

时间:2017-04-16 16:34:23

标签: python int

我在这部分遇到了问题

似乎很多人都会收到此错误

但我找不到一个我理解并解决我的问题

我发布了整个代码,所以我希望那些想要帮助的人能够发现隐藏的问题

import random
import sys

global err
global games
global avr
global letter,mults1,mults2,quo1,quo2,ans
def generate():
mults=[random.randint(100,999),random.randint(10,99)]
return mults
def cypher():
let=["A","B","C","D","E","F","G","H","I","J"];
random.shuffle(let)
return let
def display():
print letter
print "     ",check(mults1[0]),check(mults1[1]),check(mults1[2])
print "*      ",check(mults2[0]),check(mults2[1])
print "------------"
print "   ",check(quo1[0]),check(quo1[1]),check(quo1[2]),check(quo1[3])
print " ",check(quo2[0]),check(quo2[1]),check(quo2[2]),check(quo2[3])
print "------------"
print "=",check(ans[0]),check(ans[1]),check(ans[2]),check(ans[3]),check(ans[4])
#   if winthegame : return 1
#   else : return 0
def check(i):
if i in letter: return letter[i]
else : return i
def play():
inplet=raw_input('Enter the LETTER (A-J): ')
number=raw_input('Enter the matched NUMBER (0-9): ')
number=int(number)
print number
if number in letter : #TypeError argument of type 'int' is not iterable
    if inplet == letter[number] : #without the line above Shows another error #'int' object has no attribute '_getitem_' here
        print "Answer matched !"
        del letter[number]
        return true
return false

#main part
while 1:
mul=generate()
print mul
mults1=[mul[0]/100,(mul[0]/10)%10,mul[0]%10]
mults2=[mul[1]/10,mul[1]%10]
tmp1=mul[0]*mults2[1]
quo1=[tmp1/1000,(tmp1/100)%10,(tmp1/10)%10,tmp1%10]
tmp2=mul[0]*mults2[0]
quo2=[tmp2/1000,(tmp2/100)%10,(tmp2/10)%10,tmp2%10]
tmp3=mul[0]*mul[1]
ans=[tmp3/10000,(tmp3/1000)%10,(tmp3/100)%10,(tmp3/10)%10,tmp3%10]
let=cypher()
letter={0:let[0],1:let[1],2:let[2],3:let[3],4:let[4],5:let[5],6:let[6],7:let[7],8:let[8],9:let[9]}
#clean the dictionary
checker=[0,0,0,0,0,0,0,0,0,0]
i=0
while i<3:checker[mults1[i]]=1;i+=1
i=0
while i<2:checker[mults2[i]]=1;i+=1
i=0
while i<4:checker[quo1[i]]=1;i+=1
i=0
while i<4:checker[quo2[i]]=1;i+=1
i=0
while i<5:checker[ans[i]]=1;i+=1
i=0 
while i<10:
    if checker[i]==0 : del letter[i]    
    i+=1
display()
letter=input();
#game start
while 1:
    play()

非常感谢你的帮助

1 个答案:

答案 0 :(得分:0)

最终我自己解决了

    number=raw_input('Enter the matched NUMBER (0-9): ')
number=int(number)
print number
if number in letter : #TypeError argument of type 'int' is not iterable
    if inplet == letter[number] : # ERROR

我为int添加了一个新容器而不是仅仅重新签名

    numb=raw_input('Enter the matched NUMBER (0-9): ')
number=int(numb)
print number
if number in letter : #TypeError argument of type 'int' is not iterable
if inplet == letter[number] : # NO ERROR

谢谢大家的帮助