November 28th, 2012, 10:34 AM
-
My dice program
hey guys im fairly new to python and ive been studying it for 3 days now.. this is my 4th draft of my dice program and im calling it a beta release
I just wanted to know what you guys think and if there is any features that it should have
to use the program just type something like 1d6 than if you want to roll more than one dice at a time then press enter and type another set
when your done just type done and press enter
you can also type something like 2d12+4 ^^
------------------------------------------
Code:
#Dice-Roller v4
#Copyright-DreamForged.inc
#You are free to use and redistribute this product as long as credit is giving
#to the above mentioned.
#Thank you for using Dice-Roller.
import random
import time
dice_selection = []
count = 0
print "Welcome to Dice-Roller v3. To use the program read the following instructions."
print "Type in the dice you want to roll(up to a d100) then press enter."
print "For example: If you want multiple dice just hit enter and type in another set."
print "4d6"
print "1d20"
print "When your finished make sure your type prompt is on the line beneith your last"
print "dice entry. Then type done and press enter."
print "Dont type anything other than dice sets or done, the program will error out."
print "-------------------------------------------------------------------------------"
print "\n"
while True:
if count < 0:
count = 0
selection_ = raw_input("Set your dice: ")
if selection_ == "done":
print "\n"
while count > 0:
if "d" not in dice_selection[0]:
print dice_selection[0],": You forgot a d. Or you didnt type in a dice set\n"
dice_selection[:] = []
time.sleep(1)
count = 0
break;
if dice_selection[0] == "done":
for i in dice_selection:
if "done" not in dice_selection:
break;
dice_selection.remove("done")
while " " in dice_selection[0]:
if " " not in dice_selection[0]:
break;
dice_selection.remove(" ")
dice = dice_selection[0]
if "+" in dice:
indexMath_ = dice.index("+")
getMath_ = dice[indexMath_:]
Math_ = getMath_[0]
addMath_ = int(dice[indexMath_+1:])
else:
indexMath_ = dice.index("d")+4
d_position = dice.index("d")
diceCount = int(dice[0:d_position])
diceType = int(dice[d_position+1:indexMath_])
rollNum = 0
rolledList = []
rolledList[:] = []
while diceCount > 0:
roll = random.randint(1, diceType)
rollNum +=1
print rollNum,":",dice,":",roll
rolledList.append(roll)
diceCount -= 1
if diceCount == 0 and "+" in dice:
print "-----Total:",sum(rolledList) ,getMath_,":" ,sum(rolledList)+addMath_
else:
if diceCount == 0:
print "-----Total:" ,sum(rolledList)
del dice_selection[0]
count -= 1
print "\n"
if dice_selection == []:
break;
dice_selection.append(selection_)
if selection_ == "done":
count = count
else:
count += 1
November 28th, 2012, 12:25 PM
-
Code:
while " " in dice_selection[0]:
if " " not in dice_selection[0]:
break; #### ; separates statements, does not terminate them.
If a space is in dice_selection[0] how could space not be in dice selection[0]? I think you can safely remove the if condition: break statements.
Terminating a program by way of error isn't best practice. Study try: except: statements soon.
[code]
Code tags[/code] are essential for python code and Makefiles!