我在Python 3.6 TypeError中遇到错误:“列表”和“ int”的实例之间不支持“>”

时间:2018-11-07 22:30:40

标签: python python-3.6

我在Python 3.6 TypeError中遇到错误:

  在和'int'的实例之间不支持

'>'

我正在尝试从另一个Python程序中获取数据,并使用它来显示是否进行伸缩,但是我不知道其语法。我的代码粘贴在下面。

我只需要获取8位无符号整数即可与接收的数据进行比较。

import pyglet
import matplotlib
matplotlib.use('TkAgg')
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import socket
import threading
import sys
import os
import math

#Un-comment this if using OS-X.
#os.system('defaults write org.python.python ApplePersistenceIgnoreState NO')

WindowSize = 5000
SampleRate = 1000.0
VoltsPerBit = 2.5/256

#Define global variables
Fs = 1000
FlexWindowSize = 0.25
data = []
displayData = [-2 for i in range(WindowSize)]
flexing = False

# This reads from a socket.
def data_listener():
  global data
  UDP_PORT = 9000
  sock = socket.socket(socket.AF_INET, # Internet
                      socket.SOCK_DGRAM) # UDP
  sock.bind((UDP_IP, UDP_PORT))
  while True:
    newdata, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
    data.extend(list(newdata))

#Handle command line arguments to get IP address
if (len(sys.argv) == 2):
    try:
        UDP_IP = sys.argv[1]
        socket.inet_aton(UDP_IP)
    except:
        sys.exit('Invalid IP address, Try again')
else:
    sys.exit('EMG_Acquire <Target IP Address>')

#Connect the UDP_Port
UDP_PORT = 9000
sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP

print('Connected to ', str(UDP_IP))
print("Listening for incoming messages...")
print('Close Window to exit')

#Start a new thread to listen for data over UDP
thread = threading.Thread(target=data_listener)
thread.daemon = True
thread.start()

#Load and place image resources
pyglet.resource.path = ['./resources']
pyglet.resource.reindex()
ForeArm_image = pyglet.resource.image("forearm.png")
Bicep_image = pyglet.resource.image("Bicep.png")
ForeArm_image.anchor_x = 7
ForeArm_image.anchor_y = ForeArm_image.height-150
Bicep_image.anchor_x = Bicep_image.width/2
Bicep_image.anchor_y = Bicep_image.height/2

#Define the moving ForeArm class
class ForeArm(pyglet.sprite.Sprite):
  def __init__(self, *args, **kwargs):
    super(ForeArm,self).__init__(img=ForeArm_image,*args, **kwargs) 
    self.rotate_speed = 100.0
    self.rotation_upper_limit = -10
    self.rotation_lower_limit = -100
    self.rotation = self.rotation_upper_limit
    self.key_handler = pyglet.window.key.KeyStateHandler()

  def update(self, dt):
    if flexing:
      if not ((self.rotation-self.rotate_speed*dt) <=   self.rotation_lower_limit):
        self.rotation -= self.rotate_speed*dt
      else:
        self.rotation = self.rotation_lower_limit
    else:
      if not((self.rotation+self.rotate_speed*dt) >= self.rotation_upper_limit):
        self.rotation += self.rotate_speed*dt
      else:
        self.rotation = self.rotation_upper_limit

#Setup the main window
main_window = pyglet.window.Window(1000,600)
main_batch = pyglet.graphics.Batch()
background = pyglet.graphics.OrderedGroup(0)
foreground = pyglet.graphics.OrderedGroup(1)
bicep = pyglet.sprite.Sprite(img=Bicep_image,x=350,y=150,batch=main_batch,group=background)
forearm = ForeArm(x=510, y=115,batch=main_batch,group=foreground)
pyglet.gl.glClearColor(1, 1, 1, 1)
main_window.push_handlers(forearm)
main_window.push_handlers(forearm.key_handler)


def update(dt):
  global displayData, data, flexing

  newData = list(data)

  data = []
  newDisplay = list(displayData[len(newData):len(displayData)] + newData)
  displayData = list(newDisplay)

  #Put your flex algorithm code here!
  #If flexing is detected, set the 'flexing' variable to True.
  #Otherwise, set it to False. 
  #############################
  #ALL OF YOUR CODE SHOULD GO BELOW HERE

  if displayData > 20:
    flexing = True
  else: 
    flexing = False

  #ALL OF YOUR CODE SHOULD GO ABOVE HERE
  forearm.update(dt)

@main_window.event
def on_draw():
    main_window.clear()
    main_batch.draw()

   pyglet.clock.schedule_interval(update, 1/120.0)
   pyglet.app.run()

1 个答案:

答案 0 :(得分:0)

首先,您已经将newDisplay实例化为列表,因此无需执行displayData = list(newDisplay)。您可以只做displayData = newDisplay

第二,当您尝试评估displayData > 5时,您正在将列表与整数进行比较。这就像问“此列表是否大于5?”。这不合逻辑。

一个更常见的范例是询问列表的 length 是否大于某个数字。您是否要说:“如果displayData列表的长度大于20,请执行某些操作”?如果是这样,请使用if len(displayData) > 5

但是,在这些代码行中您会遇到一些更大的逻辑问题:

newDisplay = list(displayData[len(newData):len(displayData)] + newData)
displayData = list(newDisplay) 

您正在尝试在displayData定义中使用newDisplay,但是此时代码中还没有displayData

另一个人提到,如果您发布的代码足以传达您的问题以及完整的错误消息,我们可以为您提供更有效的帮助。