My first try with python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Code sharing (https://python-forum.io/forum-5.html) +--- Thread: My first try with python (/thread-4300.html) |
My first try with python - MaxAim - Aug-06-2017 So, I wanted to play D&D with my friends and I had an idea. What if I code a program to roll the dice for me? Here is my first try, I don't know anything about coding, so it kind of sucks. Any advice is welcome. Thank you all :3 import random def Random(): while True: try: dice = int(input("How many dice? ")) break except: print("That's not a valid option!") while True: try: side = int(input("How many sides? ")) break except: print("That's not a valid option!") x = 0 total = 0 while x < dice: x = x+1 rolls = random.randint (1, side) total = total+rolls print rolls print "Total =", total Random() Random() RE: My first try with python - wavic - Aug-07-2017 import random rolls = int(input('Number of rolls: ')) for roll in rolls: dice = random.choice(list(range(1,7))) print(dice) RE: My first try with python - micseydel - Aug-07-2017 The number of sides doesn't clarify the start / end points, and d100s have gaps. I would probably hard-code the different dice as options, and maybe have an input like "2d20" so that the count and type can be specified at the same time. When you catch the exception from user input, you want to catch the specific kind of exception, not a more generic one, so as not to accidentally suppress something you weren't considering. RE: My first try with python - MaxAim - Aug-07-2017 Quote:maybe have an input like "2d20"I thought about that, but I don't really know how to do that (°Д°) RE: My first try with python - nilamo - Aug-07-2017 Oh hey, D&D! I also once wrote a roller: http://dnd.nilamo.com/ It's different from what you're working on, though, as mine was made for character generation (roll 4d6, 6 times for a row, for 6 rows to make a grid, reroll all 1s, and add the three highest numbers to get a stat, then choose any row/column for your base stats). (Aug-07-2017, 08:32 PM)MaxAim Wrote: I thought about that, but I don't really know how to do thatSomething like: >>> response = input("Roll: ") Roll: 2d20 >>> number_of_dice, type_of_dice = map(int, response.lower().split("d")) >>> number_of_dice 2 >>> type_of_dice 20 |