Python Forum

Full Version: Create new variable dependent on two existing variables
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
#create trial type to determine stimulus switching vs recurrent
def TrialType(Congruency):
if 'Congruency' = 'pCongruency' return 'True'
elif 'Congruency' != 'pCongruency' return 'False'

...
else:
return '.'
I have two vairalbes: 'Congruency' and 'pCongruency' where each variable has possible values of 1,2, or 3. Using SAS I was able to create a new varialbe 'TrialType' by doing a function similar to the one above. From comments so far it seems I should have return values of true or false.

I am certain there are syntax errors. I am 12 hours into python.
(Oct-24-2020, 04:52 AM)JoeOpdenaker Wrote: [ -> ]I need to create a variable "trial_type" that tells me if "congruency" is equal to "pcongruency".

If this problem statement is correct then your function should return boolean value (either True or False).
you say you want a variable, yet you show function. Also where pCongruency comes from into the function, is it global?
There are even more problems:

1. Syntax errors for checking equality or inequality.
2. You seem to be comparing strings, rather than variables. Presumably that's not what you intended.
#create trial type to determine stimulus switching vs recurrent
def TrialType(Congruency):
if 'Congruency' = 'pCongruency' return 'True'
elif 'Congruency' != 'pCongruency' return 'False'

...
else:
return '.'



I have two vairalbes: 'Congruency' and 'pCongruency' where each variable has possible values of 1,2, or 3. Using SAS I was able to create a new varialbe 'TrialType' by doing a function similar to the one above. From comments so far it seems I should have return values of true or false.

I am certain there are syntax errors. I am 12 hours into python.
why do you need such function in a first place

spam = 2
eggs = 3
foo = 2

bar = spam == eggs
print(bar)

bar = spam == foo
print(bar)
Output:
False True
Note, these are bool values True and False, not strings "True" and "False"

and of course you can always can do, just

if spam == eggs:
    # do samething
In your if statement, change = to ==