价值不会存储

时间:2017-02-01 10:41:18

标签: python

我有一段时间有这个问题,我的一些价值观只会拒绝存储。这很可能是因为我在某个地方打破了一条规则,但是在一段时间内在网上搜索相当之后我就找不到问题了。

代码:

#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)

我收到的错误是:

NameError: name 'stancount' is not defined

我之前尝试给变量stancount一个数字,然后通过输入运行它,但是它只会在输入之前传递我给它的数字。

3 个答案:

答案 0 :(得分:1)

您必须在函数外部声明变量并在函数内部对它们进行修改,因为变量是定义它们的本地变量,因为您在函数内定义stancount它实际上是&#34;删除& #34;当你离开这个功能时:我推荐类似的东西;

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()

这会将stancount设置为您在函数中设置的值,并允许您随后将其打印

答案 1 :(得分:0)

当您尝试打印时,stancount变量超出范围。

您可以将其作为参数传递给print_mining_values()函数并以此方式打印,

所以print_mining_values()会变成:

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)

并且您的min_count_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(stancount, advcount, ultrcount)

请注意,变量现已传递到您的打印功能中。

答案 2 :(得分:0)

您可以将stancount声明为全局变量,如:

def min_count_set():
    global stancount