Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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中模拟一个监视赃物箱打开程序_Python_Python 2.7_Python 3.x - Fatal编程技术网

试图在Python中模拟一个监视赃物箱打开程序

试图在Python中模拟一个监视赃物箱打开程序,python,python-2.7,python-3.x,Python,Python 2.7,Python 3.x,因此,基本上,我正在尝试重新创建在Overwatch中打开一个战利品箱,然后在python中打开一个可运行的程序。我试图让它在一个数组中取四个随机项,并在用户每次键入“打开”打开一个框时显示它们。在每个盒子打开后,我希望它循环并询问他们是否想打开另一个盒子,或者他们不想打开,然后停止程序。以下是我目前的代码: import random # welcome print("Welcome to the Overwatch Loot Box Simulator!") OpenBox = inpu

因此,基本上,我正在尝试重新创建在Overwatch中打开一个战利品箱,然后在python中打开一个可运行的程序。我试图让它在一个数组中取四个随机项,并在用户每次键入“打开”打开一个框时显示它们。在每个盒子打开后,我希望它循环并询问他们是否想打开另一个盒子,或者他们不想打开,然后停止程序。以下是我目前的代码:

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数组将被更多的名称填充。非常感谢您的帮助

您可以将所有内容包装在
while(True)
-循环中,如果用户输入的不是open,则可以使用
break
停止循环

确保只有输入和输出在循环中,因为您不想在每次传递时重新定义列表

import random
print("Welcome to the Overwatch Loot Box Simulator!")
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']

while(True):
    OpenBox = input("Type 'open' to open a loot box! ")
    if OpenBox == "open":
        print(random.choice(OverwatchSkins))
    else:
        break

正如Christian所建议的,类似于以下内容:

import random
import sys

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']

while True:
    key = raw_input('\nType "open" to open a loot box!\n(Type "q" to exit.)\nYour input: ')
    if key.lower()=='q':
        sys.exit()
    elif key.lower()=='open':
        print random.choice(OverwatchSkins)
    else:
        print "Invalid input, try again!"

这比你要求的多一点。下面为每个项目添加了一个概率,因此选择“稀有”项目的频率低于选择“史诗”项目的频率,并且选择了4个项目

该示例已重新格式化为使用Python约定,例如变量名的
snake\u case
而不是
CamelCase

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中的其他列表构建列表的方法:

types = [skin.split(':')[0] for skin in overwatch_skins]
weightings = [frequency[type] for type in types]
您可以通过在每个调用之后放置
print()
调用,并使用
random.choices
一次返回四个加权选项来探索

通常,您会将项目的“稀有”、“史诗”和“传奇”方面与描述分开,例如使用元组。因此:

('Legendary', 'Oni Genji'),
而不是:

'Legendary: Oni Genji',

至少麻烦你把你的问题正确地格式化。可能是真棒的复制品,谢谢!你知道如何在每次打开一个“盒子”时从阵列中随机取出四件物品吗?@智囊团,看看