Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for How to build a Dice Roller in Python
Rishabh Singh ⚡
Rishabh Singh ⚡

Posted on

     

How to build a Dice Roller in Python

Hello everyone, today we are going to create aDice Roller in Python.

Alt Text

How does it work?

In the real world, we use dice to get a random number ranging from 1- 6 so we can proceed with the game accordingly. Our program is going to work similarly. Every time we will run our program, it will return us a random number between 1 & 6. Now let's jump to coding and make this project.

Let's Code

The first thing we are gonna do is import the required modules.

For this project, we need only one module which israndom module which helps us to generate random values.

importrandom
Enter fullscreen modeExit fullscreen mode

We have successfully importedrandom module.

Now we may need to use the Dice Roller multiple times hence it will be a good idea to use anwhile loop in order to keep our code running for as long as user wishes.

whileTrue:pass
Enter fullscreen modeExit fullscreen mode

Here we go! Here is the basic syntax of thewhile loop. We will substitute thatpass keyword with our remaining code.

Now let's give our code the ability to generate a random number between 1 to 6.

Here we will make use of one of the functions of ourrandom module.

print(f"The value is",random.randint(1,6))
Enter fullscreen modeExit fullscreen mode

random.randint(START_VALUE, END_VALUE) is the function we are going to use.

As previously mentioned, this function will take anSTART_VALUE & anEND_VALUE which will be1 &6 in our case, since we need a value between 1 & 6.

We are also using anprint() function along withf-string to directly print the output on the console, rather than storing it in a variable.

We are almost done!

Now let's add one line to ask the user if he/she wishes to roll the dice again or would like the end the program instead. This is pretty simple.

repeat=input("Roll Dice again?'y' for yes &'n' for no:")ifrepeat=='n':break
Enter fullscreen modeExit fullscreen mode

Here we are simply using input() function to ask the user and the user can reply in eitheryes orno. And we are using anif statement so that if the user repliesno, then the loop will terminate otherwise the loop will continue to run forever.

Finally here's how ourwhile loop will look like:

whileTrue:print("Rolling Dice...")print(f"The value is",random.randint(1,6))repeat=input("Roll Dice again?'y' for yes &'n' for no:")ifrepeat=='n':break
Enter fullscreen modeExit fullscreen mode

Here we have also added an optionalprint() function to indicate the rolling of the dice.

You did it! 🤩

Source Code

You can find the complete source code of this project here -

mindninjaX/Python-Projects-for-Beginners

Support

Thank you so much for reading! I hope you found this beginner project useful.

If you like my work please considerBuying me a Coffee so that I can bring more projects, more articles for you.

https://dev-to-uploads.s3.amazonaws.com/i/5irx7eny4412etlwnc64.png

Also if you have any questions or doubts feel free to contact me onTwitter,LinkedIn &GitHub. Or you can also post a comment/discussion & I will try my best to help you :D

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

💻 Full Stack Engineer | 🔆 Freelance UI/UX Designer | ⚡ Ambassador/Lead @microsoft @codecademy @codeforcauseIn | 👨‍🏫Teaching + ✍️Writing + 🛠Building
  • Location
    Mumbai, India
  • Education
    University of Mumbai
  • Joined

More fromRishabh Singh ⚡

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp