python - 类型错误 : argument of type 'int' is not iterable

标签 python linux

#!/usr/bin/python 

import time 
from array import *

THINKING  = 0
HUNGRY = 1
EATING = 2

class Philosopher: 
    def __init__(self):
        self.ph = array('i',[1, 2, 3, 4, 5])
        self.sleeptime = array('i',[30, 30, 30, 30, 30])

    def initialization_code(self):
        for i in range(self.ph.__len__()):
            self.ph[i] = THINKING

    def pickup(self,i):
        self.ph[i] = HUNGRY 
        self.test(i)
        if(EATING not in (self.ph[i])):
            while(EATING not in (self.ph[i])):
                time.sleep(self.sleeptime[i])

    def putdown(self,i):
        self.ph[i] = THINKING
        self.test((i+4)%5)
        self.test((i+1)%5)

    def test(self,i):
        if((2 not in (self.ph[(i+4)%5]))and(2 not in (self.ph[(i+1)%5]))and(self.ph[i]==HUNGRY)):
            self.ph[i] = EATING

    def start_process(self):
        for i in range(self.ph.__len__()):
            self.pickup(i)
            self.putdown(i)


    def display_status(self):
        for i in range(self.ph.__len__()):
            if (self.ph[i] == 0):
                print "%d is THINKING" % i+1
            elif (self.ph[i] == 1):
                print "%d is WAITING" % i+1
            elif (self.ph[i] == 2):
                print "%d is EATING" % i+1

phil = Philosopher()
phil.initialization_code()
phil.start_process()
phil.display_status()   

以上是我尝试在 python 中实现哲学家就餐问题的一段代码。 当我运行这段代码时,它显示了这个错误:

Traceback (most recent call last):

File "dining.py", line 59, in <module>

phil.start_process()

   File "dining.py", line 43, in start_process    
   self.pickup(i)    
   File "dining.py", line 27, in pickup    
   self.test(i)    
   File "dining.py", line 38, in test    
   if((2 not in (self.ph[(i+4)%5]))and(2 not in (self.ph[(i+1)%5]))and(self.ph[i]==HUNGRY)):
   TypeError: argument of type 'int' is not iterable

谁能帮我解决这个问题,为什么会显示这个错误。我搜索了它但无法解决。 提前致谢!!

最佳答案

您的等式得出整数。您不能对整数使用 in

>>> 'foo' in 3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument of type 'int' is not iterable

关于python - 类型错误 : argument of type 'int' is not iterable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11184769/

相关文章:

linux - 如何计算 ps 命令输出 bash 脚本中 rss 和 vsz 之间的关系?

linux - 调用键盘按键,通过 proc - tcl 中的过程(函数)

c - 如何设置 Gearmenu 的一个元素不敏感?

Python - 从 CSV 创建字典的字典

python - 如果发现重复项,则取列表中值的平均值

python - 如何在 python 中绘制二进制数据序列

python - Pandas 缺失值 : fill with the closest non NaN value

linux - 为什么 sed 会留下很多文件?

php - 通过 php 包装器获取原始数据的方法?抛出错误

python - 当我调用 update_idletasks() 时,Tkinter 有时会卡住