Python Forum
A CLI based Dice Roller.
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A CLI based Dice Roller.
#1
Please like if you liked my code. Tongue Tongue

import random
import time

choice = 'y' or 'n'
x = 0

input_1 = input("Press 1 for rolling a dice or 2 to exit the simulator : ")

while choice == 'y':
    if input_1 == '1':
        w = random.randint(1,6)
        print()
        print("Rolling Dice.")
        while x < 2:
            print(".")
            time.sleep(1)
            x += 1 
        print()
        print("It is",w)
        print()
        if w == 1:
            print()
            print("X X X")
            print("X 0 X")
            print("X X X")
            print()
        elif w == 2:
            print("0 X X")
            print("X X X")
            print("X X 0")
            print()
        elif w == 3:
            print("0 X X")
            print("X 0 X")
            print("X X 0")
            print()
        elif w == 4:
            print("0 X 0")
            print("X X X")
            print("0 X 0")
            print()
        elif w == 5:
            print("0 X 0")
            print("X 0 X")
            print("0 X 0")
            print()
        elif w == 6:
            print("0 X 0")
            print("0 X 0")
            print("0 X 0")
        print()
        choice = input("Do you want to roll the dice again? : ")
        print()
        x = 0
    else:
        print()
        print("Goodbye.")
        print()
        print("Coded by Muhammed©")
        time.sleep(10)
        break

while choice == 'n':
    print("Goodbye")
    print()
    print("Coded by Muhammed©")
    time.sleep(10)
    break
Please comment down about my program. Dance
#2
I like the method of showing the die with X's and 0's. But I would have written the code differently:

faces = ['X X X\nX 0 X\nX X X', '0 X X\nX X X\nX X 0', '0 X X\nX 0 X\nX X 0',
    '0 X 0\nX X X\n0 X 0', '0 X 0\nX 0 X\n0 X 0', '0 X 0\n0 X 0\n0 X 0']
print(faces[random(6)]
Much simpler than all those if/elif statements.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
#3
(Oct-24-2018, 03:48 PM)Ablazesphere Wrote:
while choice == 'n':
    print("Goodbye")
    print()
    print("Coded by Muhammed©")
    time.sleep(10)
    break
Why have a while loop, if you'll never loop more than once? Remove the break, and just use an if choice == "n": instead.
#4
(Oct-24-2018, 09:39 PM)nilamo Wrote:
(Oct-24-2018, 03:48 PM)Ablazesphere Wrote:
while choice == 'n': print("Goodbye") print() print("Coded by Muhammed©") time.sleep(10) break
Why have a while loop, if you'll never loop more than once? Remove the break, and just use an if choice == "n": instead.

Good idea but I used while loop purposely.
#5
(Oct-26-2018, 10:12 AM)Ablazesphere Wrote: Good idea but I used while loop purposely.
Doh
Would you enlighten us what the purpose is?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

#6
Guys it doesn't take much time to leave a like. Please like I would appreciate it
(Oct-26-2018, 10:15 AM)buran Wrote:
(Oct-26-2018, 10:12 AM)Ablazesphere Wrote: Good idea but I used while loop purposely.
Doh Would you enlighten us what the purpose is?

What I am trying to say is whether you use a while loop or an if statement your bound to get a same output. Right?
#7
(Oct-26-2018, 10:19 AM)Ablazesphere Wrote: What I am trying to say is whether you use a while loop or an if statement your bound to get a same output. Right?

There are many ways to do something. Don't you want to do it in the best/proper way? Also, you said you you did it purposely, i.e. there was specific reason to use while loop instead of just using the if statement. Now it just looks like you don't accept critique.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

#8
(Oct-26-2018, 10:39 AM)buran Wrote:
(Oct-26-2018, 10:19 AM)Ablazesphere Wrote: What I am trying to say is whether you use a while loop or an if statement your bound to get a same output. Right?
There are many ways to do something. Don't you want to do it in the best/proper way? Also, you said you you did it purposely, i.e. there was specific reason to use while loop instead of just using the if statement. Now it just looks like you don't accept critique.

There is nothing wrong in what you said and i am not denying any critique. I used while loop to make the code look simpler.

What was really in my mind was,

while choice == 'y':
I used while loop for 'y'.

while choice == 'n':
Why not for 'n'. Got it?
#9
Do whatever you want...

For the benefit of the other who may look at the thread
from random import choice
faces = ['X X X\nX 0 X\nX X X', '0 X X\nX X X\nX X 0', '0 X X\nX 0 X\nX X 0',
         '0 X 0\nX X X\n0 X 0', '0 X 0\nX 0 X\n0 X 0', '0 X 0\n0 X 0\n0 X 0']
while True:
    user_input = input("Press Enter to roll a dice. Press any other key to exit the simulator: ")
    if user_input:
        break
    print(choice(faces))
print("Goodbye")
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

#10
(Oct-26-2018, 11:11 AM)buran Wrote: Do whatever you want... For the benefit of the other who may look at the thread
from random import choice faces = ['X X X\nX 0 X\nX X X', '0 X X\nX X X\nX X 0', '0 X X\nX 0 X\nX X 0', '0 X 0\nX X X\n0 X 0', '0 X 0\nX 0 X\n0 X 0', '0 X 0\n0 X 0\n0 X 0'] while True: user_input = input("Press Enter to roll a dice. Press any other key to exit the simulator: ") if user_input: break print(choice(faces)) print("Goodbye")

Ill do whatever i want and please keep your stupid comments to yourself. Try to be nice sometimes.

(Oct-26-2018, 11:20 AM)Ablazesphere Wrote:
(Oct-26-2018, 11:11 AM)buran Wrote: Do whatever you want... For the benefit of the other who may look at the thread
from random import choice faces = ['X X X\nX 0 X\nX X X', '0 X X\nX X X\nX X 0', '0 X X\nX 0 X\nX X 0', '0 X 0\nX X X\n0 X 0', '0 X 0\nX 0 X\n0 X 0', '0 X 0\n0 X 0\n0 X 0'] while True: user_input = input("Press Enter to roll a dice. Press any other key to exit the simulator: ") if user_input: break print(choice(faces)) print("Goodbye")
Ill do whatever i want and please keep your stupid comments to yourself. Try to be nice sometimes.

I am so sorry that you have been offended by a silly 3 letter word.


Possibly Related Threads…
Thread Author Replies Views Last Post
  Dice roller with Tkinter GUI Yojul 1 6,251 Oct-24-2018, 01:40 AM
Last Post: ichabod801
  Dice Roller mcmxl22 4 4,068 Feb-05-2018, 09:45 AM
Last Post: buran

Forum Jump:

User Panel Messages

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