-1

Please can someone explain what's going wrong here? Unfortunately, I have been tasked to complete this using a function; otherwise, I would've used a built-in function like count() Thanks!

scores = [3,7,6,9,4,3,5,2,6,8]
y = int(input("What score are you searching for in the scores array?  "))
a = len(scores)
z = False
def count1(c,b):
    for d in range(0,c):
        if scores[d] == y:
            print("yes")
            b = True
            return(b)
            
        else:
            print("no")                      
count1(a,z)
    
if z == True:
    print(y, "occurs in the array")
else:
    print(y, "does not occur in the array")

my code^

Python 3.7.5 (tags/v3.7.5:5c02a39a0b, Oct 15 2019, 00:11:34) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
= RESTART: C:\Users\18skeffingtonc\
What score are you searching for in the scores array?  3
yes
3 does not occur in the array

the output, after entering what should be a valid input^

  • 1
    Python is pass by value, so writing `b = True` inside the function achieves nothing, as it only modifies the variable inside the function – UnholySheep Nov 25 '22 at 09:41
  • could you elaborate, what could I do to fix it? – ceskeff11 Nov 25 '22 at 09:47
  • Return a value in all code paths and then actually use it, instead of just ignoring it like you do now – UnholySheep Nov 25 '22 at 09:49
  • @ceskeff11, when you pass `z` to your `count1` function, the function takes the value of `z` assigns it to a local variable `b`, and then does stuff with `b`. Nothing actually happens to the original variable `z` - its VALUE is passed into the function and then worked with. When the function call ends, `z` remains unchanged - so when you run your `if z==True` block, `z` always evaluates to `False`. – Vin Nov 25 '22 at 09:50
  • @vin is there any way I could return the value of b and assign it to the value of z? – ceskeff11 Nov 25 '22 at 09:51
  • @UnholySheep It is not a matter of pass-by-value. Python actually passes the reference. The issue is that `=` does not modify the data in the object on the left-hand side of the equation. It changes what the variable (in this case the local variable) refers to. Either way, of course, `b = True` does not do what OP expects. – zvone Nov 25 '22 at 09:54
  • Quite honestly your entire function could just be a `return y in c` or `return y in scores` - there is no attempt at counting, so the name of the function is very confusing – UnholySheep Nov 25 '22 at 10:00

1 Answers1

0

The basic fix to your immediate problem is to modify the line count1(a, z) to read z = count1(a, z).

That way, you give z to your count1, allow count1 to modify z, and then overwrite the old value of z with the new value generated by your count1.

That said, you have a lot going on in your code that you don't really need. One concise way to do what you're attempting would be:

scores = [3,7,6,9,4,3,5,2,6,8]

def count1(scores):
    y = int(input("What score are you searching for in the scores array?  "))
    print (f'{y} is {"" if y in scores else "not "}in the array.')
    
count1(scores)

Trying this out:

What score are you searching for in the scores array?  3
3 is in the array.

What score are you searching for in the scores array?  12
12 is not in the array.
Vin
  • 929
  • 1
  • 7
  • 14