fentalyn
fentalyn

Reputation: 79

I have a Function which sometimes returns True, sometimes False. How do I count the Trues and False?

TLDR: add

print(hundred())

to this code, and you will see the function returns sometimes True, sometimes False. How do I count these with a loop? If I use an if statement, it returns all True or all False over all iterations... Inside a loop the function is either True True True True True or False False False False... which makes no sense to me.

I have battled over this for 3 days. It's about a Coin Flip problem from Automate Boring Stuff. Been "programming" for 1 month or so with no prior exp.

So, this is the function, that returns False or True. I need to be able to count them somehow. So, if the function is called 10 times (iterations variable), I need for each time it returns True to count them. I tried while loops, if statements, for loops, I don't get why it doesn't work... Really stuck.

import random


headtails = ['H', 'T']
resultlist = []
current = 1
total = []
count = 0
countlist = []
tries = 1


def hundred():
    global resultlist, current, total, count, countlist, tries, headtails
    for i in range(100):
        x = random.choice(headtails)
        resultlist.append(x)
        if resultlist[i] != resultlist[i-1]:
            current = 0
        else:
            current = current +1
            if current >= 6:
                total.append(current)
                current = 0
    if len(total) != 0:
        return True
    else:
        return False

# function ends here, now we need to call it and count Trues and Falses. 
# How do I do it? This doesn't work:

iterations = 0
number_of_true = 0
overalls = 0

while iterations < 10:
    iterations += 1

    if hundred():
        number_of_true += 1
        overalls += 1
    elif hundred() is False:
        overalls += 1
print(number_of_true, overalls)

OK I found the problem but not solution. If you call the function many times

print(hundred())
print(hundred())
print(hundred())
print(hundred())

they all going to be either False or True, which means they all point to the same value in memory. So, it's not possible to iterate its results in any way... damn, what do I do. I get a new result only when I run/stop program.

Upvotes: 0

Views: 1050

Answers (1)

Random Davis
Random Davis

Reputation: 6857

You're making two calls to hundred() each time you want to check if it's returning True or False, meaning you're not actually checking just one result, but two. Your while loop doesn't seem to have any end conditions either. Also your loop variable doesn't exist so your script throws an error. Lastly, overalls was unnecessary from what I could tell, since iterations kept track of that number anyway. Here's how I think the last section should look:

while iterations < 10:
    iterations += 1
    result = hundred()
    
    if result:
        number_of_true += 1

print(number_of_true, iterations)

When running the script, it either gives 10 10 or 0 10, which I'm not sure is what you want, but at least the loop part is correct. If you're having a problem with hundred() then I suggest asking another question about that specifically. I think the issue with it is due to you using too many globals, some of which need resetting each time you call the function.

Upvotes: 1

Related Questions