-3

This is a simple code I wrote for the flanker task:

import random

congruent = open('congruent.txt', 'r')
incongruent = open('incongruent.txt', 'r')

def createList(file):
    lst = []
    for row in file:
        lst.append(row.strip('\n'))
    return lst
    file.close()

lstCongruent = createList(congruent)
lstIncongruent = createList(incongruent)

nr_trials = 20

def middleString(txt):
    middle = txt[(len(txt)-1)//2]
    return middle
    
def checkCongruency(item):
    if item in lstCongruent:
        correctCongruentAnswers = 0
        correctCongruentAnswers += 1
        return correctCongruentAnswers
    elif item in lstIncongruent:
        correctIncongruentAnswers = 0
        correctIncongruentAnswers += 1
        return correctIncongruentAnswers
            

for i in range(nr_trials):
    rndItem = random.choice(lstCongruent + lstIncongruent)
    print(rndItem)
    answer = input('Please write your answer: ')
    if answer == 'STOP':
        exit()
    while answer != 'F' and answer != 'J':
        print('\33[91m Please type only F or J \33[0m')
        break
        
    if middleString(rndItem) == '<' and answer == 'F':
        print('Correct answer!')
        checkCongruency(rndItem)
    elif middleString(rndItem) == '>' and answer == 'J':
        print('Correct answer!')
        checkCongruency(rndItem)
    else:
        print('Incorrect answer')
 
       
print('Correct congruent answers: ', correctCongruentAnswers)
print('Correct incongruent answers: ', correctIncongruentAnswers)

But when I run it, I get:

File "main.py", line 68, in <module>
    print('Correct congruent answers: ', correctCongruentAnswers)
NameError: name 'correctCongruentAnswers' is not defined

is there any way to solve this without completely changing the code? I've tried different things, like defining the functions inside the for loop or some other stuff, but it won't work.

  • 1
    Your `checkCongruency` function will not return anything if the item is not in a list. Otherwise your function will always return 1 – David Jan 29 '22 at 23:17
  • See https://docs.python.org/3/faq/programming.html for local vs. global variables: “In Python, variables that are only referenced inside a function are implicitly global. *If a variable is assigned a value anywhere within the function’s body, it’s assumed to be a local unless explicitly declared as `global`.*” (The global variable should also be declared — read: assigned — in the global scope, probably before the functions.) – user2864740 Jan 29 '22 at 23:29
  • Welcome to Stack Overflow! Please take the [tour]. For debugging help, you need to make a [mre] including minimal code, any example input, and expected output. You can [edit]. The existing answers may help, but it looks like resetting `correctCongruentAnswers` on every loop of `for i in range(nr_trials)` doesn't make sense: you actually want to remember the total of `checkCongruency()` for every loop. – wjandrea Jan 29 '22 at 23:29
  • 1
    @user2864740 Direct link: https://docs.python.org/3/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python – wjandrea Jan 29 '22 at 23:30
  • @user2864740 Huh, that section seems to be really outdated, cause it doesn't mention the `nonlocal` scope, and it says builtins are global. – wjandrea Jan 29 '22 at 23:33
  • 1
    @wjandre Thanks for the link update (Safari Mobile copy kicks my butt). I think it’s more of a primer. Probably better versions exist.. – user2864740 Jan 29 '22 at 23:34
  • Does [this](https://stackoverflow.com/a/70888942/1658617) help? – Bharel Jan 29 '22 at 23:45

1 Answers1

-1

Since variable inside functions can only be use within the function, you can declare them as being global variables, albeit this is not good practice.

def checkCongruency(item):
    global correctCongruentAnswers #here
    global correctIncongruentAnswers #and here
    if item in lstCongruent:
        correctCongruentAnswers = 0
        correctCongruentAnswers += 1
        return correctCongruentAnswers
    elif item in lstIncongruent:
        correctIncongruentAnswers = 0
        correctIncongruentAnswers += 1
        return correctIncongruentAnswers
Robin Sage
  • 969
  • 1
  • 8
  • 24