Python Forum
A CLI based Dice Roller. - 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: A CLI based Dice Roller. (/thread-13634.html)

Pages: 1 2


A CLI based Dice Roller. - Ablazesphere - Oct-24-2018

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


RE: A CLI based Dice Roller. - ichabod801 - Oct-24-2018

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.


RE: A CLI based Dice Roller. - nilamo - Oct-24-2018

(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.


RE: A CLI based Dice Roller. - Ablazesphere - Oct-26-2018

(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.


RE: A CLI based Dice Roller. - buran - Oct-26-2018

(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?


RE: A CLI based Dice Roller. - Ablazesphere - Oct-26-2018

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?


RE: A CLI based Dice Roller. - buran - Oct-26-2018

(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.


RE: A CLI based Dice Roller. - Ablazesphere - Oct-26-2018

(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?


RE: A CLI based Dice Roller. - buran - Oct-26-2018

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")



RE: A CLI based Dice Roller. - Ablazesphere - Oct-26-2018

(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.