我在Python 3.6中得到错误类型错误:
'>' not supported between instances of 'list' and '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()
首先,您已经将
newDisplay
实例化为一个列表,因此不需要执行displayData = list(newDisplay)
。你可以做displayData = newDisplay
。在其次,当您试图计算
displayData > 5
时,您是在比较一个列表和一个整数。这就像在问,“这个列表是否大于5?”。这不合逻辑。在一个更常见的范例是询问列表的长度是否大于某个数字。你是想说“如果
displayData
列表的长度大于20,那么做点什么”?如果是,请使用if len(displayData) > 5
。在但是,在这些代码行中有一些更大的逻辑问题:
您正试图在您的}。在
newDisplay
定义中使用displayData
,但此时您的代码中还不存在{正如另一个人提到的,如果你发布足够的代码来表达你的问题,以及完整的错误信息,我们可以更有效地帮助你。在
相关问题 更多 >
编程相关推荐