0

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

manski
  • 131
  • 1
  • 10
  • 2
    Don't use `for _` if you're going to be using `_` as a variable inside the loop. `for _` is the conventional way to say that you don't care about the iteration variable. Give it a real name. – Barmar May 24 '19 at 20:12
  • You don't need the `elif` and `else` blocks. – Barmar May 24 '19 at 20:13
  • you don't have to use `elif/else` if you put only `pass` – furas May 24 '19 at 20:13
  • in `HandAnalyzer` use `return how_much_Green, how_much_Red` and then you can do `how_much_Green, how_much_Red = HandAnalyzer()` – furas May 24 '19 at 20:15

1 Answers1

0

You could use global variables like

BadHand = False
Hand = True
Hand_to_analyze = []
my_hand = ['red', 'blue', 'green']
how_much_Green = 0
how_much_Red = 0

def HandAnalyzer(hand, cards):
    global how_much_Green
    global how_much_Red
    if hand:
        Hand_to_analyze.append(cards)
        print("hand is True")

        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)
eatmeimadanish
  • 3,809
  • 1
  • 14
  • 20
  • hey thanks a lot. Do you have any idea why how_much_Red was not giving me an error, but how_much_Green was? – manski May 25 '19 at 21:47
  • They both would give the error, How_much_Green was just first, so it stopped there, unless one is defined outside of the function and you are missing it. – eatmeimadanish May 28 '19 at 17:36