Frage

I'm new to programming (about a week old). I've run into a little problem in Python, couldn't find anything helpful in the searches (new to that here as well).

The script I made below is working if I just don't use softimage's inputbox

l1 = ['Taskmaster', 'Red Skull', 'Zemo']
l2 = ['Capt. America', 'Iron Man', 'Thor']

def findguy (where, inputguy):
    a = "not here"
    for i in (range(len(where))):
        if where[i] == inputguy:
            a = "yes here"
    print a

#findguy (l1, 'Taskmaster')

x = Application.XSIInputbox ("Who are you looking for?","find character")
y = Application.XSIInputbox ('Which list?','Search List')


findguy (y, x)

If I just use findguy(type the inputs here directly) the code works, but if I try to input it through the input box in Softimage it just keeps returning "not here"

If anyone could be kind enough to help this week old struggling self learner, it would be greatly appreciated.

Thanks, Gogo

War es hilfreich?

Lösung

Your method works fine. The problem lies in the fact that the input you get from

y = Application.XSIInputbox ('Which list?','Search List')

will be a string. So y is a string and not a list. The same applies to x of course but that doesn't give an issue because your lists contain only strings.

What happens is your function call actually looks like this (example)

findguy('l1', 'Zemo')

and then on this line

if where[i] == inputguy:

it will compare the characters of l1, being l and 1 to inputguy. Of course that keeps being false so you get "not here" as output.

What you want is your function call to look like this instead

findguy(l1, 'Zemo')

This can be achieved by doing something like this:

y = Application.XSIInputbox ('Which list?','Search List')
if y == "l1":
    y = l1
elif y == "l2":
    y = l2

This simply 'picks' a list based on your input and passes that to findguy. In your situation this would be an O.K. solution but if your code was only an example and your actual code has more lists, or will have in the future you should look for a better solution. A suggestion would be to store your guys in a dictionary instead:

guys = {'l1': ['Taskmaster', 'Red Skull', 'Zemo'], 'l2': ['Capt. America', 'Iron Man', 'Thor']}

Then you can process your input with

x = Application.XSIInputbox ("Who are you looking for?","find character")
y = Application.XSIInputbox ('Which list?','Search List')

findguy (guys[y], x)

This works because the dictionary's keys are strings.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top