0

I have the following code that is designed to take 4 words from the user and search for all of them through the lists defined in the beginning, eventually returning with the list that contains all words. My issue is that when I def() a block of code, python does not replace the argument within the code, for example:

Were I to define something as:

def Search(var):
   var.Ccount(example)

Then python would not replace var.count with test.count if I were to run Search(test)

The full code is seen below:

list1 = ['q','triangle','lambda','lightning','horse','xy','cdot']
list2 = ['umlau','q','backcdot','swirl','whitestar','xy','question']
list3 = ['copyright','eye','swirl','k','r','lambda','whitestar']
list4 = ['6','paragraph','tb','horse','k','question','smiley']
list5 = ['trident','smiley','tb','cdot','paragraph','antenna','blackstar']
list6 = ['6','umlau','railroad','ae','trident','nu','omega']

is1 = 0
is2 = 0
is3 = 0
is4 = 0

symbol1 = str(input("Symbol 1: \n"))
symbol2 = str(input("Symbol 2: \n"))
symbol3 = str(input("Symbol 3: \n"))
symbol4 = str(input("Symbol 4: \n"))

def search(n):
    is1 = n.count(symbol1)
    is2 = n.count(symbol2)
    is3 = n.count(symbol3)
    is4 = n.count(symbol4)

search(list1)
if is1 ==  1 and is2 == 1 and is3 == 1 and is4 == 1:
    correctlist = list1

search(list2)
if is1 ==  1 and is2 == 1 and is3 == 1 and is4 == 1:
    correctlist = list2

search(list3)
if is1 ==  1 and is2 == 1 and is3 == 1 and is4 == 1:
    correctlist = list3

search(list4)
if is1 ==  1 and is2 == 1 and is3 == 1 and is4 == 1:
    correctlist = list4

search(list5)
if is1 ==  1 and is2 == 1 and is3 == 1 and is4 == 1:
    correctlist = list5

search(list6)
if is1 ==  1 and is2 == 1 and is3 == 1 and is4 == 1:
    correctlist = list6

print (correctlist)
Cunha
  • 7
  • 1
  • You need to read up about variable scope and functions in python - and avoid using `global` (if anything advises you to use it). See e.g. https://stackoverflow.com/questions/291978/short-description-of-the-scoping-rules – match May 07 '20 at 18:28

2 Answers2

0

You're having a different issue than you think. The problem is not with the argument to the function, but with the results. You are not updating the global isN variables as you intend, but rather, you always see the same zero values they had at the start.

There are a few ways around that issue. You could use a global statement in the function to update them. Or you could return the results from the function, rather than having it try to use global values (it would then be up to the caller to sort out where they get stored).

I'd strongly recommend the latter approach. Its especially good here because you don't actually need the separate values. You can instead call the all function on the tuple of results you get from Search:

def search(n):
    is1 = n.count(symbol1)
    is2 = n.count(symbol2)
    is3 = n.count(symbol3)
    is4 = n.count(symbol4)
    return is1, is2, is3, is4

results = search(list1)
if all(results):
    correctlist = list1

Note that this tests something slightly different than your current code. all tests if all the values are "truthy" (e.g. bool(x) is True), which for integers means all values that are not equal to zero. Your current code only accepts counts of 1, but my code will accept 2 or more. If you really do want only 1 values, then you'd want to call all on a generator expression: all(x == 1 for x in results).

Blckknght
  • 100,903
  • 11
  • 120
  • 169
0

I try to add something to code as defined global variable and change lists , it work well:

list1 = ['q','triangle','lambda','lightning','horse','xy','cdot'] 
list2 = ['umlau','q','backcdot','swirl','whitestar','xy','question'] 
list3 = ['copyright','eye','swirl','k','r','lambda','whitestar'] 
list4 = ['6','paragraph','tb','horse','k','question','smiley'] 
list5 = ['trident','smiley','tb','cdot','paragraph','antenna','blackstar'] 
list6 = ['6','umlau','railroad','ae','trident','nu','omega'] 

global is1, is2, is3, is4

is1 = 0; is2 = 0; is3 = 0; is4 = 0 

symbol1 = str(input("Symbol 1: \n")) 
symbol2 = str(input("Symbol 2: \n")) 
symbol3 = str(input("Symbol 3: \n")) 
symbol4 = str(input("Symbol 4: \n"))

def search(n): 
    global is1, is2, is3, is4
    is1 = n.count(symbol1) 
    is2 = n.count(symbol2) 
    is3 = n.count(symbol3) 
    is4 = n.count(symbol4) 

correctlist=[]

search(list1) 
if is1 == 1 and is2 == 1 and is3 == 1 and is4 == 1: 
    correctlist.append(list1)

search(list2) 
if is1 == 1 and is2 == 1 and is3 == 1 and is4 == 1: 
    correctlist.append(list2) 

search(list3) 
if is1 == 1 and is2 == 1 and is3 == 1 and is4 == 1: 
    correctlist.append(list3)

search(list4) 
if is1 == 1 and is2 == 1 and is3 == 1 and is4 == 1: 
    correctlist.append(list4) 

search(list5) 
if is1 == 1 and is2 == 1 and is3 == 1 and is4 == 1: 
    correctlist.append(list5) 

search(list6) 
if is1 == 1 and is2 == 1 and is3 == 1 and is4 == 1: 
    correctlist.append(list6) 
j=0
for lst in correctlist:
    j+=1
    print ("[{}]:{}".format(j, lst))
Alchimie
  • 426
  • 4
  • 12