簡體   English   中英

在while true循環中無法識別Python用戶定義的列表

[英]Python user defined list not being recognised within a while true loop

首先,感謝您與我成為Python世界的新手。 我正在編寫一組簡單的代碼,並且一直在絞盡腦汁了解我要去哪里。 我懷疑這是一件相對簡單的事情,但到目前為止,所有搜索都沒有結果。 如果在此之前已解決此問題,請保持警惕,我已經找了幾天!

我正在研究以下問題,並且在發現並糾正了許多問題之后,我懷疑我處於最后一個障礙:-

def main():
    our_list = []
    ne = int(input('How many numbers do you wish to enter?  '))
    for i in range(0, (ne)): # set up loop to run user specified number of time
        number=int(input('Choose a number:-  '))
        our_list.append(number) # append to our_list
    print ('The list of numbers you have entered is ') 
    print (our_list) 
main()

while True:
    op = input ('For the mean type <1>, for the median type <2>, for the mode type <3>, to enter a new set of numbers type <4> or 5 to exit')
    import statistics
    if op == "1":
        mn = statistics.mean(our_list)
        print ("The mean of the values you have entered is:- ",mn)
    if op == "2":
        me = statistics.median(our_list)
        print ("The median of the values you have entered is:- ",me)
    if op == "3":
        mo = statistics.mode(our_list)
        print ("The mode of the values you have entered is:- ",mo)
    if op == "5":
        main()
    else:
        print("Goodbye")
        break`

由於某些原因,在while true循環中無法識別附加的(our_list),從而使統計信息計算無效。 預先感謝我對缺少明顯之處的任何指導。

干杯布賴恩

我不確定“不被識別”的確切含義,但是our_listmain內部的局部變量,因此只能在main內部使用。

因此,如果嘗試在其他地方使用它,則應獲取NameError

如果您的代碼實際上具有與我們在此處未看到的局部變量同名的全局變量,則情況可能會更加令人困惑-您將不會獲得NameError ,而將獲得全局變量的值,即不是你想要的

最好的解決方案是從函數返回值,然后讓調用者使用返回的值。 例如:

def main():
    our_list = []
    ne = int(input('How many numbers do you wish to enter?  '))
    for i in range(0, (ne)): # set up loop to run user specified number of time
        number=int(input('Choose a number:-  '))
        our_list.append(number) # append to our_list
    print ('The list of numbers you have entered is ') 
    print (our_list) 
    return our_list

the_list = main()

while True:
    op = input ('For the mean type <1>, for the median type <2>, for the mode type <3>, to enter a new set of numbers type <4> or 5 to exit')
    import statistics
    if op == "1":
        mn = statistics.mean(the_list)
        print ("The mean of the values you have entered is:- ",mn)
    if op == "2":
        me = statistics.median(the_list)
        print ("The median of the values you have entered is:- ",me)
    if op == "3":
        mo = statistics.mode(the_list)
        print ("The mode of the values you have entered is:- ",mo)
    if op == "5":
        the_list = main()
    else:
        print("Goodbye")
        break

還有其他選擇-您可以傳遞一個空列表以供main填充,或使用全局變量(或者更好的是,使用更嚴格的等效項,例如類實例的屬性或閉包變量),或重構代碼,以便所有人需要訪問our_list是在同一函數內…但是我認為這是在這里完成您要執行的操作的最干凈的方法。


順便說一句,這是不太最后一關,但你非常接近:

  • 經過任何均值,中位數或眾數后,它將到達“再見”並退出,而不是返回循環。 您知道elif嗎?
  • 您在菜單中混合了'5''4'
  • 如果用戶輸入23並要求輸入模式,則您的代碼會將ValueError跟蹤轉儲到屏幕; 可能不是您想要的。 你知道try / except嗎?

這就是我所注意到的,它們都是非常簡單的添加,因此請提前祝賀。

問題是our_list是在main()函數中定義的,在main()函數范圍之外不可見。

由於您將所有內容都放在一個塊中,因此可以刪除第1行和第6行,從main()函數中獲取代碼,並將其放在與后面的代碼相同的縮進級別上。

這似乎是因為您在main()函數中定義了our_list。 您可能應該通過在main()函數外部創建它來將其定義為全局變量。

您也可以將while循環放入函數中,並將our_list作為參數傳遞給列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM