python中的键盘输入数据类型

时间:2018-08-24 06:27:54

标签: python

我目前在嵌入式项目中工作。在我的项目中,我想比较两个值。

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

每当我尝试运行代码时,它就会显示:

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

如何克服此TypeError?

代码是:

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()
>>> 

2 个答案:

答案 0 :(得分:1)

我得到TypeError: '<' not supported between instances of 'int' and 'str',并且只有Python 3.7.0,而没有Python 2.7.15。

但是,我可以使用

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

代替

if a<b:

也许您的错误(我无法复制)也已通过这种方式解决。

答案 1 :(得分:0)

您应该搜索python中的转换。

任何类型的数据类型转换都将在“ casting”标题下,您可以轻松找到示例。