# Shadow name error and Local variable not used

## Shadow name error and Local variable not used

Contents

Problem Description:

self teaching myself how to code and starting with this book and a Udemy course.

have been working on this practice project for 2 days now and I keep running into "Local variable ‘streaks’ not used when I have the streak counter placed inside the htcheck function even though I’m Cleary defining the variable, I tried placing the streak variable outside of the function at the top of the code to declare as it a global value but that still doesn’t work and then I get the "Shadow name" error.

what the code should be doing is flipping a coin 10000 times then checking for streaks of 6 then present the user with how many streaks of 6 occurred and a percent value of how often a streak of 6 was, I’m sure a lot of you have seen this question been asked before as it’s from Al Sweigart’s Automate The Boring Stuff with python 2nd edition <- I just cant find the answer to my specific error hence this post.

I just need help with figuring out why my variable ‘ streaks = 0 ‘ isn’t working as shown below. and why it doesn’t work as a global variable declared where I have heads tails and x. I would prefer a solution that keeps streaks inside the htcheck function but i’m open to any and all solutions.

Thank you in advance.

``````# Automate Python - Coin Flips Streaks
tails = []
x = 0

#Flips a coin, then stores result into respective list
def coinflip():
y = random.randint(0, 1)
if y == 0:
tails.clear()
elif y == 1:
tails.append('T')

#checks if list len is 6 then clears and adds +1 to streak
def htcheck():
streaks = 0
if len(heads) == 6:
streaks = streaks + 1
elif len(tails) == 6:
tails.clear()
streaks = streaks + 1

while x < 10000:
x = x + 1
coinflip()
htcheck()

print('# Streaks of 6: ", streaks)

``````

## Solution – 1

While you can use global variables for something like this, I generally prefer using `return` statements and tracking the variables separately for easier debugging and readability

Something like this is an option

``````# Automate Python - Coin Flips Streaks
tails = []
num_streaks = 0
#here we eliminate the need for a counter variable x by using a for loop instead of a while loop below, instead creating a counter for our streaks

#Flips a coin, then stores result into respective list
def coinflip():
y = random.randint(0, 1)
if y == 0:
tails.clear()
elif y == 1:
tails.append('T')

#checks if list len is 6 then clears and adds +1 to streak
def htcheck():
streaks = 0
if len(heads) == 6:
streaks = streaks + 1
elif len(tails) == 6:
tails.clear()
streaks = streaks + 1
#here we return the local variable instead of trying to use the global scope
return streaks

#using a for instead of a while loop is a bit of a stylistic choice but can prevent problems arising from forgetting a `break` condition or messing up counter incrementation
for x in range(1000):
coinflip()
#here we add to the streak counter we declared in the outer scope by using the local variable returned from the function
num_streaks += htcheck()

print('# Streaks of 6: ', num_streaks)
``````
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.