the problem is that you try manipulate global variables inside the function
Before that, a couple of other issues, you need to take care of.
1.
Same for rest
2. It doesn't make sense to initialize the variables, then pass them as arguments. Your function does not use the arguments you pass (except x, but see 3. below). Remove the parameters from your function
3. No need to use
count
, without declaring them as such, using global
keyword. So there is e.g. heads_count
in the global scope, as well [different] heads_count
inside the function, i.e. local scope.. Anyway, working with global variables should be avoided, unless you have really good reason to do so. And that is what you are doing - returning values from function. What you need to do, is to assign the return values (i.e. unpack the tuple you return).Before that, a couple of other issues, you need to take care of.
1.
x = 0 x = int(x)x is 0, no need to cast to
int
Same for rest
2. It doesn't make sense to initialize the variables, then pass them as arguments. Your function does not use the arguments you pass (except x, but see 3. below). Remove the parameters from your function
3. No need to use
while
, better use for
loop if you know exact number of times you want to iterate - 10import random def count(n=10): # how many times to toss, n=10 by default heads_count = 0 tails_count = 0 options = ["heads", "tails"] for i in range(n): result = random.choice(options) if result == "heads": heads_count += 1 else: tails_count += 1 return (heads_count, tails_count) def main(): heads_count, tails_count = count() print(f"heads: {heads_count} tails {tails_count}") main()Note, a lot of things can be done differently, but you get the idea
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs