简体   繁体   English

选择后从列表中删除

[英]Remove from list as choices are made

I am trying to create a program that creates a character by randomly assigning it a race, class, and stats. 我正在尝试创建一个通过随机分配种族,职业和统计信息来创建角色的程序。

However I want each stat to have a unique value. 但是我希望每个统计信息都有一个唯一的值。 So if strength is 8, then none of the other stats can be 8. How would I go about doing this? 因此,如果strength是8,那么其他任何统计都不能是8。我该怎么做呢? Do I need to delete list entries as choices are made? 选择后是否需要删除列表条目?

My code 我的密码

import random

races = ["Human", "Dwarf", "Elf"]
classes = ["Fighter", "Wizard", "Rogue"]
stats = [8, 10, 11, 12, 14, 15]

Strength = 0
Dexterity = 0
Constution = 0
Intelligence = 0
Wisdom = 0
Charisma = 0

Strength = random.choice(stats)
Dexterity = random.choice(stats)
Constution = random.choice(stats) 
Intelligence = random.choice(stats)
Wisdom = random.choice(stats)
Charisma = random.choice(stats)

race = random.choice(races)
clse = random.choice(classes)

Create a random permutation of the stats list and then assign the Strength , Dexterity etc. in order of the permutation. 创建stats列表的随机排列,然后按排列顺序分配“ Strength ,“ Dexterity等。 This has the added benefit that you won't need to reset the list before creating a new character. 这具有额外的好处,即您无需在创建新角色之前重置列表。

from random import shuffle

# ...
stats = [8, 10, 11, 12, 14, 15]
shuffle(stats)
Strength = stats[0]
Dexterity = stats[1]
Constution = stats[2]
Intelligence = stats[3]
Wisdom = stats[4]
Charisma = stats[5]

As a side note, there is no need to assign a default of 0 to Strength , etc. because they will be changed immediately afterward. 附带说明一下,不需要将默认值0分配给Strength等,因为它们将在之后立即更改。

Like you said in your question you could delete entries from the list: 就像您在问题中说的那样,您可以从列表中删除条目:

Strenght = random.choice(stats)
del stats[stats.index(Strength)]

Like it is said in the answer above me, a good solution would also be to use random shuffle . 就像我在上面的答案中所说的那样,一个好的解决方案也是使用random shuffle Plus a hint when you write for example Strength = random.choice(stats) you declare the variable Strength in that moment and give it a value so you don't have to declare it above it so you can delete all those Strength = 0 and etc. 加上一个提示,例如在编写Strength = random.choice(stats)您会在那一刻声明变量Strength并为其提供一个值,这样就不必在其上方声明它,因此可以删除所有这些Strength = 0和等等

What you showed us work for 1 character. 您显示给我们的内容只需要1个字符。 But what if we want to create hundreds of them. 但是,如果我们要创建数百个呢? In Python everything is a class and in this case a strongly recommend using it. 在Python中,所有内容都是一个类,在这种情况下,强烈建议使用它。

Look at this simple example: 看这个简单的例子:

import random
from textwrap import dedent

races = ["Human", "Dwarf", "Elf"]
classes = ["Fighter", "Wizard", "Rogue"]
stats = [ 8, 10, 11, 12, 14, 15]

class char:
    def __init__(self, races, classes, stats):
        random.shuffle(stats) # <--- this solves your problem
        self.race = random.choice(races)
        self.cls = random.choice(classes)
        self.stats = dict(zip(['Strength','Dexterity','Constution',
                               'Intelligence','Wisdom','Charisma'],stats))

    def __str__(self):
        s = dedent('''\
        Your character is a....
        Race: {}
        Class: {}
        Stats: {}''').format(self.race, self.cls, self.stats)
        return s

char1 = char(races,classes,stats) # Creates char1 based on char class
char2 = char(races,classes,stats) # Creates char2 ...

print(char1) # printing a class will call the __str__ function if it exists
print()
print(char2)

A class can hold variables and other functions and in this case we create three variables (race, cls and stats) and we add a print function to easy print what we wnat. 一个类可以容纳变量和其他函数,在这种情况下,我们创建三个变量(race,cls和stats),并添加一个打印函数以轻松打印所拥有的内容。

Returned this when I ran it: 当我运行它时返回:

Your character is a....
Race: Elf
Class: Rogue
Stats: {'Dexterity': 11, 'Charisma': 15, 'Constution': 12, 'Wisdom': 10, 'Intelligence': 14, 'Strength': 8}

Your character is a....
Race: Dwarf
Class: Fighter
Stats: {'Dexterity': 14, 'Charisma': 8, 'Constution': 12, 'Wisdom': 11, 'Intelligence': 15, 'Strength': 10}

You can store the value of random.choice in a temp. 您可以将random.choice的值存储在临时文件中。 variable and use 变量和用途

stats.remove(temp)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM