-1

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))

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

János Farkas
  • 453
  • 5
  • 14
  • 1
    At least bother to format your question correctly. – tambre Apr 20 '17 at 06:46
  • Possible duplicate of [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – WhatsThePoint Apr 20 '17 at 07:40

3 Answers3

0

You can wrap everything in a while(True)-Loop and use break to stop the loop, if the user puts in something other than open.

Make sure that only the input and output are in the loop, since you don't want to redefine your list on every pass.

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 König
  • 3,437
  • 16
  • 28
0

As suggested by Christian, something like the following:

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!"
Robbie
  • 4,672
  • 1
  • 19
  • 24
0

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.

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

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]

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'),

rather than:

'Legendary: Oni Genji',
Nic
  • 1,518
  • 12
  • 26