str对象没有属性random*python2.7*

2024-10-01 15:31:53 发布

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

我写了这篇文章,很明显我搞砸了。在

我在第57行和第38行出现了错误(实际上每当它试图给变量itemname分配一个新名称时)

import random

def createitem():

    weapon = ['sword', 'dagger', 'axe']
    armor = ['cloth armor', 'chainmail', 'leather armor', 'full plate']

    magicpre = ['shining', 'burning', 'aether', 'fine']
    magicsuf = ['of the bear', 'of the wolf', 'of the jackal']
    magictag = '*MAGIC*'

    rarepre = ['angelic', 'demonic', 'blessed', 'cursed', 'masterwork']
    raresuf = ['of heaven', 'of hell', 'of the king', 'of the chimera']
    raretag = '*RARE*'

    pendingitem = []

    itemname = ''

    appenditem = pendingitem.append(itemname)

    aq = random.randint(0,5)


    for num in range(0, aq):

        weporarm = random.randint(0,1)
        raritycheck = random.randint(0,5)
        preorsuf = random.randint(0,1)

        if weporarm == 1:
            if raritycheck == 5:
                itemname = raretag, random.choice(rarepre), random.choice(armor), random.choice(raresuf)
            elif raritycheck == 4 and preorsuf == 2:
                itemname = raretag, random.choice(armor), random.choice(raresuf)
            elif raritycheck == 4 and preorsuf == 1:
                itemname = raretag, random.choice(rarepre), random.choice(armor)
            elif raritycheck == 3:
                itemname = magictag, random.choice(magicpre), random.choice(armor). random.choice(magicsuf)
            elif raritycheck == 2 and preorsuf == 0:
                itemname = magictag, random.choice(magicpre), random.choice(armor)
            elif raritycheck == 2 and preorsuf == 1:
                itemname = magictag, randcom.choice(armor), random.choice(magicsuf)
            elif raritycheck == 1 and preorsuf == 0:
                itemname = magictag, random.choice(magicpre). random.choice(armor)
            elif raritycheck == 1 and preorsuf == 1:
                itemname = magictag, random.choice(armor), random.choice(magicsuf)
            else:
                itemname = random.choice(armor)
        else:
            if raritycheck == 5:
                itemname = raretag, random.choice(rarepre), random.choice(weapon), random.choice(raresuf)
            elif raritycheck == 4 and preorsuf == 2:
                itemname = raretag, random.choice(weapon), random.choice(raresuf)
            elif raritycheck == 4 and preorsuf == 1:
                itemname = raretag, random.choice(rarepre), random.choice(weapon)
            elif raritycheck == 3:
                itemname = magictag, random.choice(magicpre), random.choice(weapon). random.choice(magicsuf)
            elif raritycheck == 2 and preorsuf == 0:
                itemname = magictag, random.choice(magicpre), random.choice(weapon)
            elif raritycheck == 2 and preorsuf == 1:
                itemname = magictag, randcom.choice(weapon), random.choice(magicsuf)
            elif raritycheck == 1 and preorsuf == 0:
                itemname = magictag, random.choice(magicpre). random.choice(weapon)
            elif raritycheck == 1 and preorsuf == 1:
                itemname = magictag, random.choice(weapon), random.choice(magicsuf)
            else:
                itemname = random.choice(weapon)

    appenditem
    a = str(pendingitem)
    print('Your inventory:', a)

Tags: andoftherandomarmorchoiceelifweapon
1条回答
网友
1楼 · 发布于 2024-10-01 15:31:53

这两行上有一个句点(.),而不是逗号(,),这导致python将它后面的random解释为前一个变量的属性,当然,这是失败的。在

itemname = magictag, random.choice(magicpre), random.choice(armor). random.choice(magicsuf)
# Here                              -^

只要用逗号代替它,你就可以了:

^{pr2}$

相关问题 更多 >

    热门问题