gpt4 book ai didi

python - python中的键盘输入数据类型

转载 作者:行者123 更新时间:2023-12-01 09:06:34 25 4
gpt4 key购买 nike

我目前正在从事一个嵌入式项目。在我的项目中,我想比较两个值。

第一个值是整数(0),第二个值是键盘输入(也应该是整数)。

每当我尝试运行它显示的代码时:

{TypeError: unorderable types: int() < str()}

如何克服这个类型错误?

代码是:

import sys
import time
import RPi.GPIO as gpio
import collections

c = collections.Counter()
c=0


required_position=0
current_position=0
pulse_generated=0
req=0

gpio.setwarnings(False)
gpio.setmode(gpio.BCM)
gpio.setup(23,gpio.OUT)
gpio.setup(24,gpio.OUT)
gpio.setup(9,gpio.OUT)
gpio.setup(10,gpio.OUT)
gpio.setup(13,gpio.OUT)
gpio.setup(19,gpio.OUT)

mode_1=(8,10,15)
gpio.setup(mode_1,gpio.OUT)
mode_1=(17,27,22)
gpio.setup(mode_1,gpio.OUT)
resolution={
'1':(0,0,0),
'1/2':(1,0,0),
'1/4':(0,1,0),
'1/8':(1,1,0),
'1/16':(0,0,1),
'1/32':(1,0,1),
}


def current_position_calculation():
global required_position
global current_position
global pulse_generated
current_position=pulse_generated*6

def required_distance():
global required_position
global current_position
global pulse_generated
print('position of stepper motor are'
'z for homeposition(0 deg)',
'a for 10 degree',
'b for 20 degree',
'd for 30 degree',
'e for 40 degree',
'f for 50 degree',
'g for 60 degree',)

req=input("choose your required position")
required_position=req
if required_position==current_position:
gpio.output(23,True)
while current_position!=required_position:
gpio.output(24,True)
time.sleep(t)
gpio.output(24,False)
time.sleep(t)
current_position+=1
c+=1
elif current_position<required_position:
gpio.output(23,False)
while current_position>required_position:
gpio.output(24,True)
time.sleep(t)
gpio.output(24,False)
time.sleep(t)
current_position-=1
c-=1
def previous_position():
global required_position
global current_position
global pulse_generated
if current_position>required_position:
pulse_generated=required_position-currentposition
elif current_position<required_position:
pulse_generated=currentposition-required_position

while True:
current_position_calculation()
required_distance()
previous_position()

这是我的完整初始化代码。

我的代码的 mcve 是

import sys
import time

a=0
b=input("enter the value ")

if a<b:
a+=1
print(a)
else:
a-=1
print(a)

错误:

enter the value a
Traceback (most recent call last):
File "/home/pi/mcve.py", line 7, in <module>
if a<b:
TypeError: unorderable types: int() < str()
>>>

用于输入整数值:

enter the value 0
Traceback (most recent call last):
File "/home/pi/mcve.py", line 7, in <module>
if a<b:
TypeError: unorderable types: int() < str()
>>>

最佳答案

我得到TypeError: '<' not supported between instances of 'int' and 'str' ,并且仅适用于 Python 3.7.0,不适用于 Python 2.7.15。

但是,我可以用

修复该错误
if a<int(b):

而不是

if a<b:

也许你的错误(我无法重现)也可以通过这种方式得到修复。

关于python - python中的键盘输入数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51998676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com