-2

I've had this problem for a while, where some of my values will just refuse to store. It's most likely because of a rule which I am breaking somewhere, but after searching the Internet for quite some time and I can't find the problem.

Code:

#TO SET#

def min_count_set():
  stancount = int(input("How many standard miners do you wish to start with? "))
  if (stancount > 10000 or stancount < 0):
    print("\n Please enter a valid number to start with. \n \n")
    min_count_set()
  else:
    advcount= int(input("How many advanced miners do you wish to start with? "))
  if (advcount > 10000 or advcount < 0):
    print("\n Please enter a valid number to start with. \n \n")
    min_count_set()
  else:
    ultrcount = int(input("How many ultra miners do you wish to start with? "))
  if (ultrcount > 10000 or ultrcount < 0):
    print("\n Please enter a valid number to start with. \n \n")
    min_count_set()
  else:
    print("\n Returning you to the setup menu \n \n")
  set_mining_values()

#TO PRINT#
def view_mining_values():
  print("\n Printing all of the variables now.")
  print("\n Number of standard miners starting with: ")
  print(stancount)
  print("\n Number of advanced miners starting with: ")
  print(advcount)
  print("\n Number of ultra miners starting with: ")
  print(ultrcount)

The error I receive is:

NameError: name 'stancount' is not defined

I previously tried giving the variable stancount a number and then run it through the input, but then it would just relay the number I gave it before the input.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 2
    you are trying to access stancount variable outside the scope of function – Rehan Shikkalgar Feb 01 '17 at 10:43
  • lot of stuff bad here, first you dont return anything within the function, nor capture it from the recursive call, `advcount` and `ultrcount` are not shared between the calls, each call is a completly separate scope – Netwave Feb 01 '17 at 10:44
  • Those variables are defined in another function, also show us the definition of set_mining_values – Seif Feb 01 '17 at 10:44
  • Check this: http://stackoverflow.com/questions/22439752/python-local-vs-global-variables – Mohammad Yusuf Feb 01 '17 at 10:44
  • define `stancount` outside the scope of any functions, then use the functions to modify the value of `stancount` – WhatsThePoint Feb 01 '17 at 10:48
  • Suggest you read the answers to the question [_Short Description of Scoping Rules_](http://stackoverflow.com/questions/291978/short-description-of-scoping-rules). – martineau Feb 01 '17 at 12:20
  • Hi tom, because you accepted my answer can you please give it an upvote? – WhatsThePoint Feb 06 '17 at 15:57

3 Answers3

0

The stancount variable is out of scope when you try to print it.

You could pass it into the print_mining_values() function as an argument and print it that way,

So print_mining_values() would become:

def view_mining_values(sCount, aCount, uCount):
    print("\n Printing all of the variables now.")
    print("\n Number of standard miners starting with: ")
    print(sCount)
    print("\n Number of advanced miners starting with: ") 
    print(aCount) 
    print("\n Number of ultra miners starting with: ")
    print(uCount)

and your min_count_set() function would become:

def min_count_set():
    stancount = int(input("How many standard miners do you wish to start with? "))
    if (stancount > 10000 or stancount < 0):
        print("\n Please enter a valid number to start with. \n \n")
        min_count_set()
    else:
        advcount= int(input("How many advanced miners do you wish to start with? "))
    if (advcount > 10000 or advcount < 0):
        print("\n Please enter a valid number to start with. \n \n")
        min_count_set()
    else:
        ultrcount = int(input("How many ultra miners do you wish to start with? "))
    if (ultrcount > 10000 or ultrcount < 0):
        print("\n Please enter a valid number to start with. \n \n")
        min_count_set()
    else:
        print("\n Returning you to the setup menu \n \n")
    set_mining_values(stancount, advcount, ultrcount)

Notice the variables are now passed into your printing function.

RichSmith
  • 900
  • 5
  • 11
0

You will have to declare the variable outside the functions and modify them inside the functions because variables are local to where they are defined, because you are defining stancount inside a function it is then essentially "deleted" when you leave the function: I recommend something similar to this;

def min_count_set():
  stancount = int(input("How many standard miners do you wish to start with? "))
  #do stuff
  set_mining_values()
  return stancount


def view_mining_values(stancount):
  print("\n Printing all of the variables now.")
  print("\n Number of standard miners starting with: ")
  print(stancount)
  #do more stuff

stancount=min_count_set()

this will set stancount to the value you set in the function and also allow you to print it afterwards

WhatsThePoint
  • 3,395
  • 8
  • 31
  • 53
0

You could declare stancount as a global variable like:

def min_count_set():
    global stancount
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
Minion Jim
  • 1,239
  • 13
  • 30