首页 > 解决方案 > 如何避免 TypeError: '>' not supported between 'tuple' 和 'int' 实例?

问题描述

我正在创建一个模拟 Nba 球员统计数据的程序。我是初学者,不幸的是我遇到了以下错误:

TypeError: '>' not supported between instances of 'tuple' and 'int'

当数字 > 0时发生此错误:

到目前为止,这是我的代码:

import random

Malcolm_Brogdon_tendencies = { "Under_Basket_Rate" : 396, 
"Close_Left_Rate" : 32, "Close_Mid_Rate" : 50, "Close_Right_Rate" : 38, "Mid_Left_Rate" : 6, "Mid_Mleft_Rate" : 36, "Mid_Mleft" : 375, "Mid_Mid_Rate" : 47, 
"Mid_Mright_Rate" : 83, "Mid_Right_Rate" : 15, "Three_Left_Rate" : 8, "Three_Mleft_Rate" : 91, "Three_Mid_Rate" : 70, "Three_Mright_Rate" : 109, "Three_right_Rate" : 18}

Malcolm_Brogdon_Percentages = {"Under_Basket" : 487, "Close_Left" : 571, "Close_Mid" : 515, "Close_Right" : 480, "Mid_Left" : 500, "Mid_Mleft" : 375,
 "Mid_Mid" : 452, "Mid_Mright" : 564, "Mid_Right" : 400, "Three_Left" : 0, "Three_Mleft" : 350, "Three_Mid" : 261, "Three_Mright" : 319, "Three_Right" : 417}

Malcolm_Brogdon_Person = {"Shot_Attempts" : random.randint(10,16)}

do_not_include = [0]
total_shots = 0


while total_shots< Malcolm_Brogdon_Person["Shot_Attempts"]: 
    for tendencies, numbers in Malcolm_Brogdon_tendencies.items(): 
        for numbers in Malcolm_Brogdon_tendencies.items(): 
            while numbers > 0: 
                shot_distribution = random.randint(1,1001) 
                if shot_distribution not in do_not_include: 
                    do_not_include.append(shot_distribution) 
                    total_shots = total_shots + 1 
                    numbers = numbers - 1 

            
            
print (do_not_include)

我也明白从互联网导入数据会更容易,但是,我想慢慢开始。这些数字表示每个 1000 会发生多少次。然后对于其中的每一个,Malcolm_Brogdon_Tendencies它会从 1000 中生成那么多的数字,并为它们分配一个值Close_Left_Rate,例如,它会出现 32/1000 次。可能有一种更简单的方法可以做到这一点,但是,我是编程新手,不太了解。

我知道变量 numbers 是 int 中的值,Malcolm_Brogdon_Tendencies不能与 int 进行比较。有没有办法解析之前的数字所以它作为一个整数?

感谢任何提供帮助的人,因为我意识到我的描述很糟糕,而且我的代码可能会被简化很多。

标签: pythonpython-3.xtypeerror

解决方案


可能是您正在尝试访问项目中(键,值)对的数字部分,因此请尝试替换

while numbers > 0: 

while numbers[1] > 0:

推荐阅读