2017-02-01 141 views
-2

我有這個問題一段時間,其中我的一些價值觀只會拒絕存儲。這很可能是因爲我打破了某個地方的規則,但是在搜索了互聯網後相當於而且我找不到問題。價值將不會存儲

代碼:

#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一個數字,然後通過輸入運行它,但當時它只是中繼我輸入前輸入的數字。

+2

你想在功能範圍外訪問stancount變量 –

+0

很多東西都不好,首先你不會在函數中返回任何東西,也不會從遞歸調用中捕獲它,'advcount'和'ultrcount'不會在t他呼叫,每個呼叫是一個完全獨立的範圍 – Netwave

+0

這些變量是在另一個函數中定義的,也向我們展示了set_mining_values的定義 – Saksow

回答

1

您必須在函數外部聲明變量並在函數內部修改它們,因爲變量是局部的,因此您在函數內部定義了stancount,然後在離開函數時它基本上被「刪除」 :我推薦類似這樣的東西;

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您在功能設置的值,也讓你事後打印

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) 

注意變量現在傳遞到您的打印功能中。

0

你可以聲明stancount像一個全局變量:

def min_count_set(): 
    global stancount 
+0

,但他不能在本地範圍內使用'stancount'對變量進行未保存的更改 – WhatsThePoint

+0

真(對不起),但我想他想在其他地方訪問這個局部變量(因此我的答案)。 –