模拟守望先锋战利品开箱程序

模拟守望先锋战利品开箱程序

本文介绍了尝试用 Python 模拟守望先锋战利品开箱程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上我试图重新创建在守望先锋中打开一个战利品盒到 python 中的可运行程序.我试图让它在一个数组中包含四个随机项目,并在每次用户键入打开"以打开一个盒子时显示它们.在打开每个盒子后,我希望它循环并询问他们是否要打开另一个盒子,或者如果他们不想打开,然后停止程序.到目前为止,这是我的代码:

So basically I am trying to recreate opening a loot box in Overwatch into a runnable program in python. I'm trying to make it take four random items in an array and display them each time the user types 'open' to open a box. After every box is opened, I want it to loop and ask if they want to open another one, or if they don't and then stop the program. Here's my code so far:

import random

# welcome
print("Welcome to the Overwatch Loot Box Simulator!")

OpenBox = input("Type 'open' to open a loot box!")

OverwatchSkins = [
    'Legendary: Oni Genji',
    'Epic: Frostbite Pharah',
    'Rare: Banana Winston',
    'Rare: Cobalt Reinhardt',
    'Epic: Synaesthesia Lucio',
    'Legendary: Lone Wolf Hanzo',
    'Rare: Rose Widowmaker',
    'Rare: Celestial Mercy',
    'Epic: Carbon Fiber D.VA',
    'Legendary: Dr. Junkenstein Junkrat',
    'Epic: Nihon Genji',
    'Rare: Blood Reaper',
    'Rare: Ebony McCree',
    'Epic: Demon Hanzo',
    'Rare: Peridot Ana',
    'Rare: Lemonlime D.VA',
    'Epic: Taegeukgi D.VA',
    'Legendary: Mei-rry Mei',
    'Legendary: Augmented Sombra',
    'Rare: Technomancer Symmetra',
    'Rare: Mud Roadhog'
]

if OpenBox == "open":
    print(random.choice(OverwatchSkins))

OverwatchSkins 数组稍后会填充更多名称.非常感谢任何帮助!

the OverwatchSkins array would just be filled up with more names later on. Any help is greatly appreciated!

推荐答案

这超出了您的要求.下面为每个项目添加一个概率,因此稀有"项目的选择频率低于史诗"项目,并选择了 4 个项目.

This is a little more than you asked for. The following adds a probability to each item, so that 'Rare' items are chosen less often than 'Epic', and 4 items are chosen.

该示例已重新格式化为使用 Python 约定,例如 snake_case 而不是 CamelCase 用于变量名称.

The example has been reformatted to use Python conventions, eg snake_case rather than CamelCase for variable names.

import random

overwatch_skins = [
    # skin list here
]

frequency = {
    'Legendary': 2,
    'Rare': 1,
    'Epic': 4
}
# type is to the left of the first colon
types = [skin.split(':')[0] for skin in overwatch_skins]
# map types onto weightings
weightings = [frequency[type] for type in types]

print("Welcome to the Overwatch Loot Box Simulator!")
while True:
    reply = input("Type 'open' to open a loot box!")
    if reply != "open":
        break
    print(random.choices(overwatch_skins, weightings, k=4))

>>> python choices.py
['Legendary: Dr. Junkenstein Junkrat', 'Rare: Ebony McCree', 'Epic: Frostbite Pharah', 'Epic: Demon Hanzo']
['Epic: Taegeukgi D.VA', 'Rare: Lemonlime D.VA', 'Legendary: Mei-rry Mei', 'Epic: Nihon Genji']

注意列表推导式的使用,这是一种从 Python 中的其他列表构建列表的方法:

Note the use of list comprehensions, a way of building lists from other lists in Python:

types = [skin.split(':')[0] for skin in overwatch_skins]
weightings = [frequency[type] for type in types]

您可以通过将 print() 调用放在每个调用之后进行探索,并使用 random.choices 一次返回四个加权选项.

which you can explore by putting print() calls after each, and the use of random.choices to return four weighted choices at a time.

通常,您会将项目的稀有"、史诗"和传奇"方面与描述分开,例如使用元组.所以:

Normally you would keep the 'rare', 'epic' and 'legendary' aspects of an item separate from the description, eg using tuples. So:

('Legendary', 'Oni Genji'),

而不是:

'Legendary: Oni Genji',

这篇关于尝试用 Python 模拟守望先锋战利品开箱程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 14:46