1

I am currently working in an embedded project. In my project I want to compare two values.

The first value is an integer(0) and the second one is the keyboard input (and it also should be an integer).

Whenever I try running the code it shows:

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

How to overcome this TypeError?

The code is:

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

here is my full code with initialisation.

The mcve of my code is

import sys
import time

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

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

error:

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

for entering integer value:

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()
>>> 
10
  • 1
    Please extend your minimal reproducible example to actually demosntrate the problem. Provide initialisations of the used variables and drop any unrelated code, e.g. gpio(). Commented Aug 24, 2018 at 6:30
  • Be careful with indentation, e.g. around elif. Be careful with non-code, e.g. "thank you guys!!". Commented Aug 24, 2018 at 6:31
  • The MCVE is important, because when I try to recreate your problem with the given information, I get a different error TypeError: '<' not supported between instances of 'int' and 'str'. Commented Aug 24, 2018 at 6:33
  • is there any way to get keyboard input as an integer?? Commented Aug 24, 2018 at 6:39
  • Yes there is. Please make a more minimal MCVE to provide a foundation for an efficient answer. Commented Aug 24, 2018 at 6:40

2 Answers 2

1

I get TypeError: '<' not supported between instances of 'int' and 'str', and only with Python 3.7.0, not with Python 2.7.15.

However, that error I can fix with

if a<int(b):

instead of

if a<b:

Maybe your error (which I cannot reproduce) gets fixed that way, too.

Sign up to request clarification or add additional context in comments.

Comments

0

You should search for casting in python.

Any type of data type conversion will go under the heading "casting" and you can easily find examples.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.