Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 返回a<;类别';类型'&燃气轮机;使用random.choices()_Python_Oop_Scripting - Fatal编程技术网

Python 返回a<;类别';类型'&燃气轮机;使用random.choices()

Python 返回a<;类别';类型'&燃气轮机;使用random.choices(),python,oop,scripting,Python,Oop,Scripting,我们目前正在为学校编写一个小游戏,为了选择哪个通电会产生,我们使用choices()方法对概率进行加权,并从包含类名的列表中进行选择。我面临的问题是,由于choices()方法返回一个数组,我似乎无法将我的对象附加到列表中,下面是代码,如果您有时间帮助我们。。。 多谢各位 def Choice2(): Type2 = choices([Healboost, DmgsUp, FireRateBoost, ""],weights=proba_powerups) #[0][1][2] are

我们目前正在为学校编写一个小游戏,为了选择哪个通电会产生,我们使用choices()方法对概率进行加权,并从包含类名的列表中进行选择。我面临的问题是,由于choices()方法返回一个数组,我似乎无法将我的对象附加到列表中,下面是代码,如果您有时间帮助我们。。。 多谢各位

def Choice2():
    Type2 = choices([Healboost, DmgsUp, FireRateBoost, ""],weights=proba_powerups) #[0][1][2] are my class names, [3] is for no powerup
    CreatePowerUps(str(Type2)) # converting to str in order to strip "[]"


def CreatePowerUps(a):
    global Time,powerups
    a = a.strip("[]") # removing the "[]"
    print(f"a : {type(a)}") # prints "a : <class 'str'>"
    if Time > 0:
        if a == Healboost :
            powerups.append(a())
        elif a == FireRateBoost :
            powerups.append(a())
        elif a == DmgsUp :
            powerups.append(a())
        elif a == "":
            pass
        if Time > 1200:
            Time -= 10
        print(powerups) # debugging purpose (list stays empty)
        NFP.after(Time,Choice2)
    else:
        pass
def Choice2():
Type2=选项([Healboost,DmgsUp,FireRateBoost,“],weights=proba#u powerups)#[0][1][2]是我的类名,[3]表示无通电
CreatePowerUps(str(Type2))#转换为str以剥离“[]”
def CreatePowerUps(a):
全球时间,加电
a=a.strip(“[]”)#删除“[]”
打印(f“a:{type(a)}”)#打印“a”:
如果时间>0:
如果a==Healboost:
powerups.append(a())
elif a==FireRateBoost:
powerups.append(a())
elif a==DmgsUp:
powerups.append(a())
elif a==“”:
通过
如果时间>1200:
时间-=10
打印(通电)#调试目的(列表为空)
NFP.之后(时间,选择2)
其他:
通过

你到底为什么要把这个列表变成一个字符串?您只需要选择第一个元素

下面是一个简化的示例:

from random import choices

x, y, z = [0, 1, 2]
powerups = []

def choice2():
    type2 = choices([x, y, z, ""], weights=[1] * 4)
    create_power_ups(type2[0])

def create_power_ups(a):
    if a in (x, y, z):
        powerups.append(a)
    print(powerups)

choice2()
运行几次,您将看到所有不同的选项:
[]
[0]
[1]
[2]


顺便说一句,应该为类名保留大写字母,所以我在那里的时候修复了这个问题。

您需要提供一个。我可以看到这段代码有一些问题,但目前还不清楚如何用您给出的内容来修复它。看看你是否需要更多的建议。实际上,nvm,这似乎是一个非常简单的修复。我会写一个答案,但是a仍然会有帮助。你为什么要在这里把
Type2
转换成
str
CreatePowerUps(str(Type2))
?评论说:“去掉括号”,但是为什么你需要去掉括号呢?哇,我真的很惊讶,我甚至没有意识到我可以通过Type2[0]作为参数,这真的显示了我是多么的一个编码初学者。感谢你这么快的回复,我真的需要在我的代码中实现PEP8规则。