Posts: 10
Threads: 3
Joined: Apr 2020
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))
Posts: 1,950
Threads: 8
Joined: Jun 2018
Sep-21-2020, 02:31 PM
(This post was last modified: Sep-21-2020, 02:32 PM by perfringo.)
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.
Posts: 10
Threads: 3
Joined: Apr 2020
Sep-21-2020, 02:58 PM
(This post was last modified: Sep-21-2020, 03:00 PM by kraco.)
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
Posts: 1,950
Threads: 8
Joined: Jun 2018
Sep-21-2020, 03:50 PM
(This post was last modified: Sep-21-2020, 03:50 PM by perfringo.)
(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.
Posts: 10
Threads: 3
Joined: Apr 2020
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()
Posts: 1
Threads: 0
Joined: Nov 2024
Nov-25-2024, 07:07 AM
(This post was last modified: Nov-29-2024, 06:59 AM by Kiddodera.)
You could handle this by using a simple if-else structure to check the result of the dice roll and print the corresponding text.
If you’re looking for a quick tool to simulate rolls while testing or just for ideas, a dice roller online can be super handy. It’s simple to plug the results into your code and tweak from there.
Posts: 379
Threads: 2
Joined: Jan 2021
Your program will work as expected if you put your lines to generate the random dice number inside of the values function like this:
def values():
# 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))
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))
Posts: 1,145
Threads: 114
Joined: Sep 2019
This appears to be an ancient thread but, going to give my 2 cents.
from random import choice
die = [n for n in range(2,13)]
def values():
random_number = choice(die)
match random_number:
case n if n in[2,3]:
number = n
case n if n in [4,5,6]:
number = n
case n if n in [7,8]:
number = n
case n if n in [10,11]:
number = n
case 12:
number = n
case _:
number = 'error'
return f'This is number {n}. The random number is {random_number}'
print(values())
|