Python Forum
Create new variable dependent on two existing variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create new variable dependent on two existing variables
#1
#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.
buran write Oct-24-2020, 10:00 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
(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).
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
you say you want a variable, yet you show function. Also where pCongruency comes from into the function, is it global?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
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.
Reply
#5
#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.
Reply
#6
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
In your if statement, change = to ==
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create X Number of Variables and Assign Data RockBlok 8 870 Nov-14-2023, 08:46 AM
Last Post: perfringo
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,524 Nov-07-2023, 09:49 AM
Last Post: buran
  Modify an Energy Model to account for year dependent interest rate rather than only t giovanniandrean 0 398 Oct-10-2023, 07:00 AM
Last Post: giovanniandrean
Question How create programmatically variables ? SpongeB0B 6 1,215 Aug-19-2023, 05:10 AM
Last Post: SpongeB0B
  loop (create variable where name is dependent on another variable) brianhclo 1 1,101 Aug-05-2022, 07:46 AM
Last Post: bowlofred
  Create array of values from 2 variables paulo79 1 1,056 Apr-19-2022, 08:28 PM
Last Post: deanhystad
  How to create 2 dimensional variables in Python? plumberpy 5 1,791 Mar-31-2022, 03:15 AM
Last Post: plumberpy
  how to easily create a list of already existing item CompleteNewb 15 3,379 Jan-06-2022, 12:48 AM
Last Post: CompleteNewb
  is it pythonic to add a name 'attribute' to an existing variable? amjass12 6 2,143 Oct-04-2021, 09:33 PM
Last Post: amjass12
Question Calling on a Variable using other Variables jacknewport 4 1,956 Jul-23-2021, 04:18 PM
Last Post: jacknewport

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020