StealthPython StealthPython - 1 year ago 98
Python Question

Python 'if' statement - if in [string] == 'string' -- Not working?

I'm trying to find a letter in a giving string. Here's what I got:

if i in steps == 'u':
y -= pixels_per_move
elif i in [steps] == 'r':
x -= pixels_per_move

steps is a randomly generated string consisting of u, d, r, l.
So it's something like 'uuurlluddd'

So i'm just looking for those letters in 'steps' but with the code above I'm getting the error

TypeError: 'in ' requires string as left operand, not int

Which I don't understand because steps is not an int, and i've tried putting str() around it in multiple places but same error occurs. What am I doing wrong here?

Answer Source

What it is saying is that i is an int. You are testing whether i is in steps which is a string. The mostly likely thing I think you are trying to do is get the ith character of the steps in which case you should use if steps[i] == 'u':

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download