0

Why is my code only reading the first condition and not checking for the second condition on my code.

def primary():
    if keyPress.positive:    
        if raySensor.hitObject['primaryWeapon']:
            own['primary'] = raySensor.hitObject['weaponName']
        elif raySensor.hitObject['secondaryWeapon']:
            own['secondary'] = raySensor.hitObject['weaponName']

primary()            
David Buck
  • 3,752
  • 35
  • 31
  • 35
  • 1
    Did you mean for it to be `raySensor.hitObject['secondaryWeapon']` instead of `raySensor.hitObject['primaryWeapon']`? – duckboycool Apr 03 '21 at 17:15
  • yes I did not notice that. I already edited the code. – Jonel Ian Dumancas Bulao Apr 03 '21 at 17:46
  • I'm not sure how make this work, basically I just wanted the ray.Sensor to check if it is either a 'primary' or a 'secondary' then do something. – Jonel Ian Dumancas Bulao Apr 03 '21 at 18:01
  • Is `raySensor.hitObject['primaryWeapon']` always truthy? What are its possible values? Is it possible to have both true at the same time? – K.Cl Apr 03 '21 at 18:15
  • To clarify, do you consider `if keyPress.positive` to be your first condition, or are you just referring to the `if / elif` conditions ? It is best to create a reproducible example (one people can cut / paste to reproduce the issue), and provide output and expected output. – steveb Apr 03 '21 at 18:38
  • It should only have one value at a time. I mean it should either be a 'primary' or a 'secondary' both primary and secondary would output different values. " if ray.Sensor hits an object with a property "primary" it would do stuff otherwise if ray.Sensor hits an object with property "secondary" it would do another stuff. – Jonel Ian Dumancas Bulao Apr 03 '21 at 18:41
  • I'm only having problems with if/elif, I'm not yet to fond of other methods on how to structure my code. – Jonel Ian Dumancas Bulao Apr 03 '21 at 18:45

1 Answers1

0

After a day. Finally I've found a solution. It think the problem is with me using Boolean instead of a string. It got me thinking so hard and I had to try out a lot of ideas on how to fix this. Anyway thank you guys for you participation.