#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Making a (specific) dice rolling program


    Hi there, i've become interested in learning a little Python, mainly because the (small amount) of javascript that I know isn't able to create the program that i'd like to make.

    I play a table top game similar to Warhammer called Heroscape. This game uses special dice that have pictures instead of numbers, as well as traditional 20 sided dice.

    I'd like to write a program that asks for you to input the number of dice you need, and then return the result of your dice roll.

    So far I've come up with this:
    Code:
    diceNo = raw_input("How many dice do you need? ") 
    diceRoll = ['skull', 'skull', 'skull', 'symbol', 'shield', 'shield']
    from random import choice
    for diceNo print choice(diceRoll) 
    raw_input("Press Enter to continue...")
    The problem i've run in to is getting the print part to actually use the user input at the top...I just can't get my head around which loop to use. I did mess around with a for loop, but I got lost very quickly. Any help would be greatly appreciated!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    raw_input returns a string,
    you probably need an integer.

    Unless you care about error trapping, try this:
    Code:
    number_of_dice = int(raw_input('prompt'))
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    EDIT: I've completed my program, it now rolls whichever dice you choose. One last thing i'd like to ask about: How can I get it to loop back to the start and ask the first question again? Can i make the else at the end do that?
    EDIT AGAIN: Figured that question out, too. For those wondering, the code is below:
    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.')
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    You've done ok. This is a little shorter.
    Code:
    import random
    
    while True:
        diceChoose = raw_input('What sort of dice would you like to roll? (D20 or Special) ')
        if diceChoose == 'D20':
            print random.randint(1,21),
            print
        elif diceChoose == 'Special':
            try:
                diceNo = int(raw_input('How many dice do you need? '))
            except:
                continue
        	diceRoll = ['skull', 'skull', 'skull', 'symbol', 'shield', 'shield']
            for die in range(diceNo):
        		print random.choice(diceRoll)
            print'YOUR DICE ROLLS HAVE COMPLETED'
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by b49P23TIvg
    You've done ok. This is a little shorter.
    Code:
    import random
    
    while True:
        diceChoose = raw_input('What sort of dice would you like to roll? (D20 or Special) ')
        if diceChoose == 'D20':
            print random.randint(1,21),
            print
        elif diceChoose == 'Special':
            try:
                diceNo = int(raw_input('How many dice do you need? '))
            except:
                continue
        	diceRoll = ['skull', 'skull', 'skull', 'symbol', 'shield', 'shield']
            for die in range(diceNo):
        		print random.choice(diceRoll)
            print'YOUR DICE ROLLS HAVE COMPLETED'
    Brilliant, thank you very much!

IMN logo majestic logo threadwatch logo seochat tools logo