Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old December 26th, 2003, 05:20 PM
ClickForth ClickForth is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 16 ClickForth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 15 m 35 sec
Reputation Power: 0
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
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 = 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?

Reply With Quote
  #2  
Old December 26th, 2003, 06:16 PM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 6
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

Reply With Quote
  #3  
Old December 26th, 2003, 07:05 PM
ClickForth ClickForth is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 16 ClickForth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 15 m 35 sec
Reputation Power: 0
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.

Reply With Quote
  #4  
Old December 26th, 2003, 07:16 PM
ClickForth ClickForth is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 16 ClickForth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 15 m 35 sec
Reputation Power: 0
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

Reply With Quote
  #5  
Old December 26th, 2003, 07:42 PM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 6
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
Attached Files
File Type: py dice_roller.py (997 Bytes, 282 views)

Reply With Quote
  #6  
Old December 26th, 2003, 08:02 PM
ClickForth ClickForth is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 16 ClickForth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 15 m 35 sec
Reputation Power: 0
Figured out my problem:

I had sum = input("> ")

when I needed

sum = raw_input("> ")



Thanks for the help!

Reply With Quote
  #7  
Old December 26th, 2003, 10:52 PM
oxygenthief oxygenthief is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 35 oxygenthief User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
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()

Reply With Quote
  #8  
Old December 27th, 2003, 01:50 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #9  
Old December 27th, 2003, 09:49 AM
oxygenthief oxygenthief is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 35 oxygenthief User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Quote:
Originally posted by netytan
[B]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!

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:
Everyone stop what your doing and use randint() instead , as O2 spotted randrange gives you the number up to but not including 'stop'.

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.

Reply With Quote
  #10  
Old December 27th, 2003, 11:34 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #11  
Old December 27th, 2003, 02:29 PM
oxygenthief oxygenthief is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 35 oxygenthief User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Quote:
Originally posted by netytan
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
>>>

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

Reply With Quote
  #12  
Old December 29th, 2003, 07:07 PM
ClickForth ClickForth is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 16 ClickForth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 15 m 35 sec
Reputation Power: 0
Thanks for the help guys!

I'll try to go back tonight and change some of the stuff that you mentioned.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > I need help with a D&D dice roller (pretty new to python)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |