Python Forum
havent programmed in years - confused by why RETURN is not returning
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
havent programmed in years - confused by why RETURN is not returning
#3
the problem is that you try manipulate global variables inside the function 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 - 10

import 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

Reply


Messages In This Thread
RE: havent programmed in years - confused by why RETURN is not returning - by buran - Mar-25-2023, 08:17 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  String int confused janeik 7 1,200 Aug-02-2023, 01:26 AM
Last Post: deanhystad
  I am confused with the key and value thing james1019 3 1,032 Feb-22-2023, 10:43 PM
Last Post: deanhystad
  Pandas confused DPaul 6 2,686 Sep-19-2021, 06:45 AM
Last Post: DPaul
  is and '==' i'm confused hshivaraj 6 2,806 Sep-15-2021, 09:45 AM
Last Post: snippsat
  Confused with 'flags' tester_V 10 5,097 Apr-12-2021, 03:03 AM
Last Post: tester_V
  Simple Tic Tac Toe but I'm confused Izith 1 2,286 Sep-26-2020, 04:42 PM
Last Post: Larz60+
  I am really confused with this error. Runar 3 3,118 Sep-14-2020, 09:27 AM
Last Post: buran
  Confused on how to go about writing this or doing this... pythonforumuser 3 2,565 Feb-10-2020, 09:15 AM
Last Post: snippsat
  'Age' categorical (years -months -days ) to numeric Smiling29 4 3,033 Oct-17-2019, 05:26 PM
Last Post: Smiling29
  Create a monthly mean column in multiple years fyec 1 4,095 Jun-21-2018, 03:57 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020