簡體   English   中英

使用max()時獲取“'builtin_function_or_method'對象不可迭代”

[英]Getting “'builtin_function_or_method' object is not iterable” when using max()

我正在嘗試建立一個字典,允許用戶輸入名稱和相應的分數(添加游戲名稱也將是一個獎勵),然后能夠查詢高分。

這是我嘗試過的:

scores = {}

while True:                                                                                                 
    name = input("Please give me the name of the player [q to quit]:")
    if name == 'q':
        break
    else:
        grade = input("Give me their score: ")
        scores[name] = grade

highScore = max(scores.values)

for k, v in scores.items():
    if v == highScore:
        print(v, k)

這是我得到的錯誤:

highScore = max(scores.values)
TypeError: 'builtin_function_or_method' object is not iterable

max接受一個可迭代的,但是您將其傳遞給一個函數。

print(type(scores.values))   # <class 'builtin_function_or_method'>
print(type(scores.values())) # <class 'dict_values'>

僅傳遞函數的輸出,而不傳遞函數本身。

highScore = max(scores.values())

暫無
暫無

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

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