|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
I need help with a D&D dice roller (pretty new to python)
Ok, this is what I have:
import random print "D&D Dice Roller" print "1 4-Sided Die" print "2 6-Sided Die" print "3 8-Sided Die" print "4 10-Sided Die" print "5 12-Sided Die" print "6 20-Sided Die" sum = input("> ") if sum == 1: number = random.randrange(1, 5) print "The 4-Sided Die Rolled", number elif sum == 2: numberr = random.randrange(1, 7) print "The 6-Sided Die Rolled", numberr elif sum == 3: numbber = random.randrange(1, 9) print "The 8-Sided Die Rolled", numbber elif sum == 4: nuumber = random.randrange(1, 11) print "The 10-Sided Die Rolled", nuumber elif sum == 5: nnumber = random.randrange(1, 13) print "The 12-Sided Die Rolled", nnumber elif sum == 6: nummber = random.randrange(1, 21) print "The 20-Sided Die Rolled", nummber raw_input = input("Press Enter to exit") I put it one number over on each because it wasn't rolling the highest number. This is the problem I need answered: After you roll once, it displays "press enter to exit" but instead I want it to say "press enter to exit or (some key) to go to main menu" How would I loop it like that? |
|
#2
|
|||
|
|||
|
yea ... range(a,b) and randrange(a,b) gives numbers from a to one less than b.
Code:
import random
def dice_roller():
print "D&D Dice Roller"
print
print
print "1 4-Sided Die"
print "2 6-Sided Die"
print "3 8-Sided Die"
print "4 10-Sided Die"
print "5 12-Sided Die"
print "6 20-Sided Die"
sum = raw_input("> ")
if sum == '1':
numberr = random.randrange(1, 5)
print "The 4-Sided Die Rolled", numberr
elif sum == '2':
numberr = random.randrange(1, 7)
print "The 6-Sided Die Rolled", numberr
elif sum == '3':
numbber = random.randrange(1, 9)
print "The 8-Sided Die Rolled", numbber
elif sum == '4':
nuumber = random.randrange(1, 11)
print "The 10-Sided Die Rolled", nuumber
elif sum == '5':
nnumber = random.randrange(1, 13)
print "The 12-Sided Die Rolled", nnumber
elif sum == '6':
nummber = random.randrange(1, 21)
print "The 20-Sided Die Rolled", nummber
while 1:
input = raw_input("Press Enter to continue or q to quit").upper()
if input == 'Q': break
elif input == '' : dice_roller()
# elif input == something: go to main menu
|
|
#3
|
|||
|
|||
|
Thanks Yogi!
I can use that for my calculator too! EDIT: It is having a problem with dice_roller since its not defined..I'll post back in a second if I fix it. |
|
#4
|
|||
|
|||
|
Hmmm....still not working, this is what I changed and it still isn't:
while 1: input = raw_input("Press c to continue or q to quit").upper() if input == 'Q': break elif input == 'C': dice_roller() # elif input == dice_roller: go to main menu |
|
#5
|
|||
|
|||
|
It works for me.
have you 1. put #!/usr/bin/python as the first line of the program ? 2. made sure the indents are all lined up correctly ? this works for me (file attatched) Eli |
|
#6
|
|||
|
|||
|
Figured out my problem:
I had sum = input("> ") when I needed sum = raw_input("> ") Thanks for the help! |
|
#7
|
|||
|
|||
|
Just a few comments. I wouldn't use "input" as a variable name as its a keyword (input = raw_input). Also, you can cut out quite a few print statements using a dictionary to map choice numbers to die spots:
Code:
import random
dice = {1:4, 2:6, 3:8, 4:10, 5:12, 6:20}
def dice_roller():
print "D&D Dice Roller\n\n"
for die in dice:
print die, dice[die], "-Sided Die"
sum = input("> ")
num = random.randrange(1, dice[sum] + 1)
print "The %d-Sided Die Rolled %d" % (dice[sum], num)
while 1:
inp = raw_input("Press Enter to continue or q to quit: ").upper()
if inp == "Q": break
elif inp == "": dice_roller()
|
|
#8
|
||||
|
||||
|
Hi all,
actually O2 input is a __builtin__ function not a keyword, by giving a variable a name that is already in use you overwrite it until the variable is deleted. This said, it is perfectly legal and at times useful to use said a __builtin__ functions name, if your not using it anywhere in your program that is! Everyone stop what your doing and use randint() instead , as O2 spotted randrange gives you the number up to but not including 'stop'.Anyway gotta run, i'll post an example latter! Mark. |
|
#9
|
||||
|
||||
|
Quote:
As you mentioned, it *is* legal to overwrite a builtin or keyword, but if you subconsciously do it and forget about it, it can cause problems later esp. if you have a program with a lot of code and you've forgotten (or didn't realize) you overrode that function/builtin (speaking from experience, or would it be inexperience ;-)) Quote:
I did realize the stop points on randrange, but I didn't realize there was a randint Thanks for the headsup (I really need to get into looking at whole modules, instead of just key parts that I reuse). You could rewrite the randrange line as:Code:
num = random.randint(1, dice[sum]) randint is inclusive of the start and stop points, as randrange includes the start but not the stop. |
|
#10
|
||||
|
||||
|
You seem to have missed the whole point that these are not keywords; they are __builtin__ functions
and as we know functions can be overwriten! If you try to overwrite a keyword you get something like this..>>> for = str() SyntaxError: invalid syntax >>> print = isinstance SyntaxError: invalid syntax >>> for being our keyword and str() being a function. Anyway i've made a few alterations to your example. Code:
#!/usr/bin/env python
from random import randint
dices = {1: 4, 2: 6, 3: 8, 4: 10, 5: 12, 6: 20}
def die():
try:
print '\nD&D Dice Roller\n'
for dice in dices:
print '%d) %02d sided die' % (dice, dices[dice])
use = input('\n>>> ')
num = randint(1, dices[use])
return 'Rolled %d using %d sided die!' % (num, dices[use])
except:
return '\nOops, try again!'
while True:
use = raw_input('\nPress Enter to continue or \'Q\' to quit: ').upper()
if use == 'Q':
break
print die()
Ah, Pythons standard library is so extensive you couldnt possibly remember everything so dont worry about it O2! As you use them more you'll learn whats available and where ![]() Mark. |
|
#11
|
|||
|
|||
|
Quote:
You are correct, I misunderstood--I didn't realize that you could not override keywords. And, for refernece/testing, we have the keyword module: http://www.python.org/doc/current/l...le-keyword.html |
|
#12
|
|||
|
|||
|
Thanks for the help guys!
I'll try to go back tonight and change some of the stuff that you mentioned. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > I need help with a D&D dice roller (pretty new to python) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|