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
Question Calling on a Variable using other Variables jacknewport 4 279 Jul-23-2021, 04:18 PM
Last Post: jacknewport
  Create variable and list dynamically quest_ 12 886 Jan-26-2021, 07:14 PM
Last Post: quest_
  How do I get the in3.Client() to create a variable with attributes? (in3 pypi pckge) Johno 2 466 Jan-21-2021, 02:49 AM
Last Post: Johno
  Print variable values from a list of variables xnightwingx 3 692 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Assignment of non-existing class variable is accepted - Why? DrZ 6 1,052 Jul-13-2020, 03:53 PM
Last Post: deanhystad
  Python scipy odeint: solving with solution-dependent functions etienne 0 729 Jun-05-2020, 01:29 PM
Last Post: etienne
  Create, assign and print variables in loop steven_tr 10 1,459 May-28-2020, 04:26 PM
Last Post: ndc85430
  change array elements dependent on index SchroedingersLion 1 765 Nov-22-2019, 06:25 AM
Last Post: scidam
  Can I use iteration to create variable names? Mark17 8 2,328 Oct-17-2019, 06:05 AM
Last Post: perfringo
  How to create an autocomplete method that search an existing trie in python nanoprogrammer 0 1,141 Jul-20-2019, 12:09 AM
Last Post: nanoprogrammer

Forum Jump:

User Panel Messages

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