I am trying to count how many times the string 'red' appears in this list using a for-loop. However, the way it is written, I am still getting a total of zero every time I try to print the count of how_much_red.
Also, for some reason, no matter what I do, it also says that '''name 'how_much_Green' is not defined''', even though I name it in the same place as I do red.
Basically, I don't understand why it isn't working. I'm new to Python and I think I'm just misunderstanding where the for-loop is looking.
I am trying to make a program where I generate a random list, analyze to check if it meets a certain condition (glorified True/False statement) and then search that list for the presence of a string or object, and add that to a global counter. I want to be able to do this for something like 1000 randomized lists and record what appears in them each time, so the count would have to be global and cumulative
I have tried to put my counting variables for the colors outside of the function, but then python gave me error messages saying 'local variable referenced before assignment.'
BadHand = False
Hand = True
Hand_to_analyze = []
my_hand = ['red', 'blue', 'green']
def HandAnalyzer(hand, cards):
if hand:
Hand_to_analyze.append(cards)
print("hand is True")
how_much_Green = 0
how_much_Red = 0
for _ in my_hand:
if 'green' == _:
how_much_Green +=1 #this is where I usually get my error
if 'red' == _:
how_much_Red +=1 #apparently this is fine???
elif 'red' != _:
pass
elif 'green' != _:
pass
elif hand:
pass
else:
pass
def repeater():
#this is just used as a way for the program to do this multiple times
for ThisManyTimes in range(4):
HandAnalyzer(Hand, my_hand)
repeater()
print(Hand_to_analyze)
print(how_much_Red)
print(how_much_Green)
I would expect for the readout for the last 2 print statements to be: 4 4
But they are actually: 0 Name Error: 'how_much_Green' is not defined