Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with dice roll program
#1
Please help me with this dice roll program.
I am trying to make program witch checks for dice roll result and then writes a line, when an number was rolled write text see example under:
EXAMPLE Results: Dices Rolled : 2 This is a test
if dice 5 was rolled show text: My dad has a black car
I am stuck and cant figure it out, please help
Regards
Kraco


# to get random number from list
# using random.randrange
import random

# initializing list
Dice_list = [2, 3, 4, 5, 6, 7, 9, 10, 11, 12]

# using random.randrange() to
# get a random number
rand_idx = random.randrange(len(Dice_list))
random_num = Dice_list[rand_idx]

# printing random number
print("Dices Rolled : " + str(random_num))
# EXAMPLE Results: Dices Rolled : 2 This is a testa
# result dice number, print text:
# if 2 or 3 was dice result, show dice number and print text this i a test
if "2,3" in (str(random_num)):
  print('This is a test')
#if 4 or 5 was dice result, show dice number and  print text My dad has a black car
if "4,5" in (str(random_num)):
  print('My dad has a black car')
# if 6 or 7 or 8 was dice result, show dice number and print text My mother is from japan
if "6,7,8" in (str(random_num)):
  print('My mother is from japan')
#if 9 or 10 was dice result, show dice number and print text My father is from USA
if "9,10" in (str(random_num)):
  print('My father is from USA')
# if 11 or 12 was dice result, show dice number and print text My brother is from Sweden
if "11,12" in (str(random_num)):
  print('My brother is from Sweden')

#print("The string after adding number is  : " + str(res))
Reply
#2
Some things to consider:

- follow variable naming conventions
- use random.choice() instead of dealing with indices and lengths
- use <, >, = operators to make value comparison, no need to convert into string*
- use f-strings print(f'Dice rolled: {random_num}')

*your comparison probably not work the way you expect:

>>> '2,3' in '2'
False
>>> '2' in '2,3'
True
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
Hi, thanks for answering perfinog.
I am new to python, and I am under early learning process
Not sure what you ment with true \ false statement.
I have tried = and == operators, but couldnt make it work.
I am trying to create a program witch checks the number of dice rolls for example if the roll was 2 or 3 in total, lets say 3, write text "This is a test"
Can someone please fix\rewrite the code for me or give med examples how to do it?
Appreciate all the help I get
Regards
Kraco
Reply
#4
(Sep-21-2020, 02:58 PM)kraco Wrote: Not sure what you ment with true \ false statement.

In your code you wrote:

# if 2 or 3 was dice result, show dice number and print text this i a test
if "2,3" in (str(random_num)):
  print('This is a test')
What would happen if dice result is '2'? Nothing. It will never be True. It will never print anything (this is true for the whole chain of if-s you have written). If you want to test then you should do opposite: is '2' in '2,3'.

Maybe some ideas can be drawn from this code snippet (try it with different integers as num values):

if num < 2:
    print(f'{num}: too small')
elif num == 2:
    print(f'{num}: this is two')
elif 2 < num < 5:
    print(f'{num}: between 2 and 5')
else:
    print(f'{num}: really big number')
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
Thanks for all the help perfringo, your tips worked, the code works in pycharm without GUI.
But, when I move this code to run wit tkinter for gui, I get two problems:
1. When I click on button roll dice, the same result shows up, its stuck and cant reset the die roll until i close and open the program window, then it stuck again wit different die roll result.
2. The Diece result text only shows in Pycharm text window, how can I make this text visible in the program window?
Please help

# to get random number from list
# using random.randrange
import tkinter
from PIL import Image, ImageTk
import random

# top-level widget which represents the main window of an application
root = tkinter.Tk()
root.geometry('400x400')
root.title('Dice Roll')

# initializing list
Dice_list = [2, 3, 4, 5, 6, 7, 9, 10, 11, 12]

# using random.randrange() to
# get a random number
rand_idx = random.randrange(len(Dice_list))
random_num = Dice_list[rand_idx]

def values():
    # printing random number
    # print("Dices Rolled : " + str(random_num))
    if (random_num == 2):
        print("This is number two " + str(random_num))
    elif (random_num == 3):
        print("This is number three " + str(random_num))
    elif (random_num == 4):
        print("This is number four " + str(random_num))
    elif (random_num == 5):
        print("This is number five " + str(random_num))
    elif (random_num == 6):
        print("This is number six " + str(random_num))
    elif (random_num == 7):
        print("This is number seven " + str(random_num))
    elif (random_num == 8):
        print("This is number eight " + str(random_num))
    elif (random_num == 9):
        print("This is number nine " + str(random_num))
    elif (random_num == 10):
        print("This is number ten " + str(random_num))
    elif (random_num == 11):
        print("This is number eleven " + str(random_num))
    elif (random_num == 12):
        print("This is number twelve " + str(random_num))
    else:
        print("Dices Rolled : " + str(random_num))

# adding button, and command will use rolling_dice function
#button = tkinter.Button(root, text='Roll the Dice', fg='blue', command=rolling_dice)
button1 = tkinter.Button(root, text='Roll Dices', width=25, command=values)
button1.pack()

button2 = tkinter.Button(root, text='Stop', width=25, command=root.destroy)
button2.pack()

# call the mainloop of Tk
# keeps windows open
root.mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Dice Roll (Find out how many rolls until specified streak) DustinKlent 4 3,917 Jun-13-2021, 09:44 AM
Last Post: Gribouillis
  simple dice roll Byzas 1 2,318 Mar-21-2019, 02:29 AM
Last Post: ichabod801
Photo roll of the dice kyle007 0 1,695 Mar-11-2019, 01:58 AM
Last Post: kyle007
  unit test roll die saladgg 5 4,122 Nov-06-2018, 11:39 PM
Last Post: stullis
  Issue with my 'roll the dice simulation'-exercise (cannot break out of the loop) Placebo 2 3,467 Sep-30-2018, 01:19 PM
Last Post: Placebo
  Making a percentile dice roller and dice roller Fixer243 2 3,200 Sep-30-2018, 12:18 PM
Last Post: gruntfutuk
  Random Dice roll program th3h0bb5 1 5,512 Oct-18-2016, 09:25 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020