Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

pyFlakes Error

Dear Forum,

I am experiencing a pyFlakes syntax error that I cannot really make sense of. I defined the following list that is used as parameter for a function randomizing the position of my canvas elements:

positions = [1, 2]

When I try to run the code, I keep getting a pyFlakes syntax error that is referring to the above list.

Do you have any idea what the problem could be?

Thank you so much!

Best,

Sarah

Comments

  • HI @sarah_alexandra ,

    This line of code as such is valid. Can you post the exact error message (Traceback) from the console that you get when trying to run the code and the context of the code (i.e. is it part of an inline_script item)?

    — Sebastiaan

  • Hi Sebastiaan,

    Thanks a lot for your fast reply! I already figured it out, the problem was a missing bracket in the code before.

    Best,

    Sarah

  • edited October 2022

    One additional question not directly relating to the above problem but I was hoping that you could help me with this:

    I defined a list of dicts ("states") and a function ("determine_state"), the first containing all conditions for my experiment and the latter is intended to randomize the order of conditions.

    Unfortunately, there seems to be an issue with the function or the index referring to the states in the dict [l] - I keep getting an error that "l" is either not defined or of "None Type". I was thinking that "l" might have to be defined before the function like a variable, but was not sure how to do so since it is not a normal variable.

    Do you have any idea how I could solve this? My code is below, any tips highly appreciated :)

    import random
    # different trials / conditions:
    states = [
        {
         "step_size" : 0.6,
         "exit_rule" : 0.2,
         "upper_safe_amount" : 4.5,
         "lower_safe_amount" : 4.5,
         "upper_lottery_amount" : 9,
         "lower_lottery_amount" : 1,
         "expected_value" : 0,
         "upper_probability" : 0, 
         "lower_probability" : 0, 
         "color_rect" : "#919092",
         "color_expected_value" : "black",
         "color_probability" : "black",
         "response_list" : [],
         "number_decisions_trial": 0,
         "list_number_decisions_trial" : [],
         "payouts_per_trial" : [],
         "active": True
        }
        
        ,{
         "step_size" : 0.15,
         "exit_rule" : 0.05,
         "upper_safe_amount" : 4.5,
         "lower_safe_amount" : 4.5,
         "upper_lottery_amount" : 6,
         "lower_lottery_amount" : 4,
         "expected_value" : 0,
         "upper_probability" : 0,
         "lower_probability" : 0,
         "color_rect" : "#919092",
         "color_expected_value" : "black",
         "color_probability" : "black",
         "response_list" : [],
         "number_decisions_trial": 0,
         "list_number_decisions_trial" : [],
         "payouts_per_trial" : [],
         "active": True
        }
    ]
    
    # function determining the trial (only active ones)
    def determine_state(states):
        l = random.randint(0,len(states) - 1)
        if states[l]["active"]:
            return
        if not states[l]["active"]:
            while not states[l]["active"]:
                l = random.randint(0,len(states) - 1)
                if states[l]["active"]:
                    return
                else:
                    l = random.randint(0,len(states) - 1)     
    l = determine_state(states)  
    
  • Hi Sarah,

    You don't return anything. Can you try: `return I` in your function?

    Eduard

    Buy Me A Coffee

  • edited October 2022
    def determine_state(states):
        l = random.randint(0,len(states) - 1)
        while not states[l]["active"]:
            l = random.randint(0,len(states) - 1)
            if states[l]["active"]:
               return l   
    

    simplified version of your function

    Buy Me A Coffee

  • Hi Eduard,

    Thanks a lot for your fast help! I tried it out and it returns a "1" sometimes, but other times the following error occurs: "'int' object is not subscriptable"

    Do you have any idea what the issue could be?

    Thanks in advance!

    Best,

    Sarah

  • Could you provide the complete error message?

    Buy Me A Coffee

  • edited October 2022

    Now it seems to work, the error was the following:

    Traceback (most recent call last):

    File "<string>", line 62, in <module>

    File "<string>", line 52, in determine_state

    TypeError: 'int' object is not subscriptable

    In the meantime, a different issue has come up - I defined a break rule for each of the conditions - when this break rule becomes true for one of the conditions, the condition should not be shown any more. To implement this, I defined two functions: (1) defining when the entire loop ends (all conditions are not active any more); (2) when one condition is not active any more because the break rule has been met for this condition. Unfortunately, the functions do not seem to work, since the loop goes on and violates the break rules I have defined (e.g. the safe amount of one condition becomes smaller than the lower lottery amount). Do you have an idea what the problem could be? Here is the relevant piece of code:

    def at_least_one_active(states):
        ''' 
        Checks whether at least one stair case is still active
        '''
        for state in states:
            if state["active"]:
                return True
        return False
    
    def update_activity(states):
        '''
        Sets activity to False for stair cases that have reached
        their termination criteria
        '''
        for state in states:
            if (state["step_size"] < state["exit_rule"] or state["upper_safe_amount"] > state["upper_lottery_amount"] or state["upper_safe_amount"] < states[l]["lower_lottery_amount"]):
                    state["active"] == False
                    return False
    # beginning of loop where depending on the participant's answer, the parameters that are part of the break rule (e.g. step_size, upper_safe_amount) are modified. I run the function update_activity(states) in the end of the loop.
    while at_least_one_active(states):
    


  • That part:

        for state in states:
            if (state["step_size"] < state["exit_rule"] or state["upper_safe_amount"] > state["upper_lottery_amount"] or state["upper_safe_amount"] < states[l]["lower_lottery_amount"]):
                    state["active"] == False
                    return False
    

    only changes the local variable state and returns a False. If you want to change the list states, you have to return a new list with the respective field modified. For example:

        for stateI, state in enumerate(states):
            if (state["step_size"] < state["exit_rule"] or state["upper_safe_amount"] > state["upper_lottery_amount"] or state["upper_safe_amount"] < states[l]["lower_lottery_amount"]):
                    states[stateI]["active"] == False
                    return states
    

    Does that work?

    Buy Me A Coffee

  • Hi Eduard,

    Thanks for the recommendation! Unfortunately, it does not work yet. I was as well googling and thinking that it could have something to do with local vs. global variable. What does the phrase

    for stateI, state in enumerate(states):
    

    exactly do?

    And maybe for context, state refers to a single dict that is part of the following list of dicts:

    states = [
        {
         "step_size" : 0.6,
         "exit_rule" : 0.2,
         "upper_safe_amount" : 4.5,
         "lower_safe_amount" : 4.5,
         "upper_lottery_amount" : 9,
         "lower_lottery_amount" : 1,
         "expected_value" : 0,
         "upper_probability" : 0, 
         "lower_probability" : 0, 
         "color_rect" : "#919092",
         "color_expected_value" : "black",
         "color_probability" : "black",
         "response_list" : [],
         "number_decisions_trial": 0,
         "list_number_decisions_trial" : [],
         "payouts_per_trial" : [],
         "active": True
        }
        
        ,{
         "step_size" : 0.15,
         "exit_rule" : 0.05,
         "upper_safe_amount" : 4.5,
         "lower_safe_amount" : 4.5,
         "upper_lottery_amount" : 6,
         "lower_lottery_amount" : 4,
         "expected_value" : 0,
         "upper_probability" : 0,
         "lower_probability" : 0,
         "color_rect" : "#919092",
         "color_expected_value" : "black",
         "color_probability" : "black",
         "response_list" : [],
         "number_decisions_trial": 0,
         "list_number_decisions_trial" : [],
         "payouts_per_trial" : [],
         "active": True
        }
    ]
    

    Could this as well have to do with the way I am referring to the key "active"? Because in the rest of my code, I access my variables with states[l]["name_dict_key"].

    Thank you so much in advance!

  • Have you stored the return value of that function in the variable states again? The objects states in the function update_activity and states in your main script are different things. Changing it in the function will have no effect on the object outside the function unless you overwrite it like this:

    states = update_activity(states)

    If this is not it. You can try to find out whether changing the field worked, by printing out the list before and after the if statement (a.k.a. make sure your logic works).

    Buy Me A Coffee

  • edited October 2022

    Hi Eduard,

    I tried this and unfortunately, it still does not work. I as well created a simplified example to check whether the function updates the "active" keys in the dicts, and it seems that this does not work out yet:

    Simplified function + changing keys + printing:

    states = [
        {"active" : True,
         "step_size": 1,
         "exit_rule": 0.5},
        {"active" : True,
         "step_size": 2,
         "exit_rule": 1}
         ]
    
    def update_activity(states):
      '''
      Sets activity to False for stair cases that have reached
      their termination criteria
      '''
      for state in states:
        if (state["step_size"] < state["exit_rule"]):
            state["active"] == False
            return states
    
    print(states) 
    states[0]["step_size"] -= 0.7
    states[1]["step_size"] -= 0.5
    states = update_activity(states)
    print(states)
    Print output:
    [{'active': True, 'step_size': 1, 'exit_rule': 0.5}, {'active': True, 'step_size': 2, 'exit_rule': 1}]
    [{'active': True, 'step_size': 0.30000000000000004, 'exit_rule': 0.5}, {'active': True, 'step_size': 1.5, 'exit_rule': 1}]
    

    So it seems like the step size has been changed and for the first dict the exit rule step_size < exit_rule is met, however "active" is still True. So it seems like the function is not correctly updating the "active" key, right?

  • `state["active"] == False
    

    That should be a single equal sign. Can you try fixing that?

    Eduard

    Buy Me A Coffee

  • Yes this seems to work now, thank you so much!!

  • edited November 2022

    Hi Eduard,

    I tested a lot over the day and there is unfortunately still an issue I am experiencing with this function


    def update_activity(states):
        '''
        Sets activity to False for stair cases that have reached
        their termination criteria
        '''
        for state in states:
            if (state["step_size"] < state["exit_rule"] or state["upper_safe_amount"] > state["upper_lottery_amount"] or state["upper_safe_amount"] < state["lower_lottery_amount"]):
                    state["active"] = False
                    return state
    

    Unfortunately, it does only sometimes seem to update the value of the key "active" in the original dict / list, other times the original dict / list value for "active" is still "True", even though the function returns False.

    I guess I was a little too fast earlier, could it be possible that the function still does not assign the value False globally to the dict / list?

    Any ideas highly appreciated :)

    Thank a lot and best wishes,

    Sarah

  • Hi Sarah,

    Unfortunately, it does only sometimes seem to update the value of the key "active" in the original dict / list, other times the original dict / list value for "active" is still "True", even though the function returns False.

    That sounds as if it works inconsistently (sometimes it does, sometimes it doesnt). In such a case, the most probable case for the problem is that your logic doesn't work. So you need to find out how your variables interact and when exactly the if statement evaluates to true and when to false. Print statements can help with that procedure, also splitting up the long if statement into shorter ones.

    The good news is though, that the experiment works in principle.

    Let me know if you need more pointers on how to approach it.

    Eduard

    Buy Me A Coffee

  • Hi Eduard,

    Thank you so much for the reply - your suggestions are always so helpful! I figured out a workaround and now it seems to work :)

    Best,

    Sarah

Sign In or Register to comment.