I'm making a dice roller for the game Heroscape (So we can play with offshore friends). The dice for heroscape have 6 sides. 3 sides show skulls, 1 side has a symbol and 2 sides have a shield.
I've made it randomly generate 1 of those sides, but i'd like it to list off the results at the end (ie You rolled 6 skulls, 2 symbols and 4 shields).
Heres my current code:
loop = 1
while loop == 1:
diceChoose = raw_input('Pick your dice. (Press 1 for a D20 roll, and 2 for attack /defense dice.) ')
if diceChoose == ('1'):
import random
for x in range(1):
print random.randint(1,21),
print
raw_input("YOUR DICE ROLL(S) HAVE COMPLETED. PRESS ANY KEY TO CONTINUE.")
elif diceChoose == ('2'):
diceNo = int(raw_input('How many dice do you need? '))
testvar = 0
diceRoll = ['skull', 'skull', 'skull', 'symbol', 'shield', 'shield']
from random import choice
while testvar != diceNo:
print choice(diceRoll)
testvar = testvar + 1
if testvar == diceNo:
print ('YOUR DICE ROLLS HAVE COMPLETED')
raw_input("PRESS ANY KEY TO CONTINUE.")
else: loop = raw_input('Type 1 or 2. Nothing else will work. Press 1 to start the program again.')
What i've tried is a bunch of if statements, but i've realised that if I try print ('diceRoll') all I get is the whole array instead of the randomly selected dice roll.
I'm not sure how to save each diceRoll as it happens, so I can print that number later on.
(My thinking is something like
if diceRoll == 'skull' skullNo +1
print('skullNo'))
I think a good data structure for you to use for this problem would be the Counter
from the collections
module in the standard library. It works a little like a dictionary, mapping objects to an integer count. However, you can add values to it and that will increment their count.
So, I'd do this:
from random import choice
from collections import Counter
diceNo = int(raw_input('How many dice do you need? '))
diceValues = ['skull', 'skull', 'skull', 'symbol', 'shield', 'shield']
counter = Counter()
counter.update(choice(diceValues) for _ in range(diceNo))
print("Rolls:")
for value, count in counter.items():
print("{}: {}".format(value, count))
print ('YOUR DICE ROLLS HAVE COMPLETED')
raw_input("PRESS ANY KEY TO CONTINUE.")
The key line is counter.update(choice(diceValues) for _ in range(diceNo))
. This calls counter.update
with a "generator expression", which produces diceNo
random roll results. If you haven't learned about generator expressions yet, I suggest checking them out, as they can come in really handy.
Welcome to SO. I changed a few parts of your code to make them more similar to my style - feel free to change them back if that helps.
Change your loop code to simply:
while True:
This accomplishes the same thing, except that it is more pythonic.
Then, tab in one and tell set number values for the skull, symbol
and shield
. In this case, we set it to 0.
skull = 0
symbol = 0
shield = 0
Next, change the code below the diceNo = int(raw_input('How many dice do you need? '))
to this. It should be indented by one.
for x in xrange(diceNo):
import random
choice = random.randint(1,6)
if choice == 1:
skull +=1
elif choice == 2:
skull +=1
elif choice == 3:
skull +=1
elif choice == 4:
symbol =+ 1
elif choice == 5:
shield += 1
elif choice == 6:
shield += 1
This code repeats for the number of dice rolls needed. I then adds 1 to the variable associated with that name.
Directly below that, we display the info to the user.
print "You rolled %d skulls, %d symbols, and %d shields. Congrats." % (skull, symbol, shield)
Since you seemed to be having issues with the code, I decided to post it in its entirety.
while True:
#Here we set the variables
skull = 0
symbol = 0
shield = 0
diceChoose = raw_input('Pick your dice. (Press 1 for a D20 roll, and 2 for attack / defense dice.) ')
if diceChoose == '1':
import random
for x in range(1):
print random.randint(1,21),
print
raw_input("YOUR DICE ROLL(S) HAVE COMPLETED. PRESS ANY KEY TO CONTINUE.")
elif diceChoose == '2':
diceNo = int(raw_input('How many dice do you need? '))
for x in xrange(diceNo):
import random
choice = random.randint(1,6)
if choice == 1:
skull +=1
elif choice == 2:
skull +=1
elif choice == 3:
skull +=1
elif choice == 4:
symbol =+ 1
elif choice == 5:
shield += 1
elif choice == 6:
shield += 1
print "You rolled %d skulls, %d symbols, and %d shields. Congrats." % (skull, symbol, shield)
raw_input("PRESS ANY KEY TO CONTINUE.")
else: loop = raw_input('Type 1 or 2. Nothing else will work. Press 1 to start the program again.')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.