Python选项3给出了参数

2024-07-02 12:58:09 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,如果你输入

Type: Item
Total: 1,2 or 3
Item Type: Head

它“应该”循环,从特定类型的选项列表中选择,然后选择更多,如果可以的话,并确保它们不是重复的。你知道吗

import random

firstb = input('Type: ')
rarPropin = input('Total: ')
rarPropTotal = int(rarPropin)
first_random_item = input('Item Type: ')
item_slots = ['Head', 'Earring', 'Necklace']
HeadPropList = ['Stat1', 'Stat2', 'Stat3']
HeadWeight = [1, 0.5, 0.25]
EarPropList = ['Stat4', 'Stat5', 'Stat6']
EarWeight = [1, 0.5, 0.25]
NeckPropList = ['Stat7', 'Stat8', 'Stat9']
NeckWeight = [1, 0.5, 0.25]
ItemPropList = [HeadPropList, EarPropList, NeckPropList]
ItemWeightList = [HeadWeight, EarWeight, NeckWeight]
ItemProp = {Li: Prop for (Li, Prop) in zip(item_slots, ItemPropList)}
ItemWeight = {Li: Prop for (Li, Prop) in zip(item_slots, ItemWeightList)}
PropRoll = ItemProp[first_random_item]
Propstr = str(PropRoll)
PropWeight = ItemWeight[first_random_item]
Weightstr = str(PropWeight)

if firstb == "Item":
    S = []
    l = len(S)
    while l < rarPropTotal:
        c = random.choice([Propstr], [Weightstr])
        S.append(c)
        while S.contains(c):
            c = random.choice(Propstr, Weightstr)
            S.append(c)
    print("Property List: " + S)

但我不断发现以下两个错误:

Error 1: TypeError: choice() takes 2 positional arguments but 3 were given
Error 2: ValueError: The number of weights does not match the population

Tags: inputtyperandomliitemheadtotalfirst
3条回答

choice是一个方法,不是一个正则函数(而是一个什么的方法?见下文)。因此,它将隐式参数作为第一个参数,将序列作为第二个参数。您试图将这些选项作为单独的参数提供。正确的电话是

c = random.choice([Propstr, Weightstr])

那么为什么random.choice是一种方法呢?random模块创建一个random.Random实例,用于模块级的“函数”,而choice只是该实例的绑定方法,而不是函数本身。你知道吗

random.py的底部,您会发现:

# ...
_inst = Random()
# ...
choice = _inst.choice

这意味着choice的定义与

def choice(seq):
    ...

而是

def choice(self, seq):
    ...

这就是导致choice的混乱信息的原因,它要求2个参数,但给出3个参数。你知道吗

之所以发生TypeError,是因为您试图将两个参数传递给choice方法,而它只需要一个参数(隐式地加上self参数)。你知道吗

if firstb == "Item":
    S = []
    l = len(S)
    while l < rarPropTotal:
        c = random.choice([Propstr], [Weightstr])  # error happens here!
        S.append(c)
        while S.contains(c):
            c = random.choice(Propstr, Weightstr)  # and here
            S.append(c)
    print("Property List: " + S)

线条应如下所示:

c = random.choice([Propstr, Weightstr])

您的代码需要大量修改才能工作:

if firstb == "Item":
S = []
l = len(S)
while l < rarPropTotal:
    c = random.choice([Propstr, Weightstr])
    S.append(c)
    d = random.choice([Propstr, Weightstr])
    while d == c:
        d = random.choice([Propstr, Weightstr])
    S.append(d)
    l = l + 1
print("Property List: " + str(S))

为了解释,首先我在外部while循环的末尾添加了递增l的终止条件。然后我将调用改为random.choice,从变量列表中进行选择。然后我重写了如何检查重复项,因为您最初的方法根本没有这样做。c总是在S列表中,因为您将它添加到了那里。打印时还需要将S列表转换为字符串。你知道吗

对数据运行上述代码时的输出示例:

> Type: Item
> Total: 3
> Item Type: Head

Property List: ['[1, 0.5, 0.25]', "['Stat1', 'Stat2', 'Stat3']", "['Stat1', 'Stat2', 'Stat3']", '[1, 0.5, 0.25]', '[1, 0.5, 0.25]', "['Stat1', 'Stat2', 'Stat3']"]

编辑:根据您的评论,我认为您应该这样做:

if firstb == "Item":
    S = []
    l = len(S)
    while l < rarPropTotal:
        c = random.choices(PropRoll, PropWeight)[0] #[0] to avoid nested lists
        if c not in S:
            S.append(c)
            l = l + 1
    print("Property List: " + str(S))

这样可以确保没有重复项。主要的变化是它不再像在原始代码中那样使用字符串了,因为不能对编码为字符串的列表执行random.choice。这就是为什么它使用你的PropRollPropWeight。另一件事是,如果你想使用权重,你必须使用choices()方法,而不是choice()former可以使用权重,后者不能。你知道吗

样本输出:

> Type: Item
> Total: 2
> Item Type: Head

Property List: ['Stat1', 'Stat3']

相关问题 更多 >