Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 9, 2023 07:08 pm GMT

Create basic Dice Rolling Game in Python

Today, I will show you how to create a basic python Dice Rolling Game. For this project I used a simple website that doesn't need any signing in, here is the link if you want to follow along. https://www.online-python.com/

I your in a rush here is the final code: https://replit.com/@romans1234/Dice-Game-Simplified?v=1

Now let's get started. Because I am using Online Python, we must start with a simple line of code to export the random function used to roll the dice.
from random import randint

This is optional but we can create two basic print lines to welcome our user to the program.
print("Welcome to Dice Rolling Game")
print("Intiating Game")

After this we can assign multiple variables to store data, we will need a variable for the number the user will guess (we will use the input function.) The we can create a variable for each die the computer will role. Lastly, we will have a variable to give the total of both dice rolled.

guess = input("Your Guess: ")die1 = randint(1,6)die2 = randint(1,6)total = die1 + die2

Our final step is then to create an if statement. In the if statement we have to tell if the user go the answer correct, and if not correct we will need to tell them the correct answer.

if total == guess:    print("Correct!")else:    print("Incorrect!")    print(total)

Okay, guess we are all done. For python their is a bunch of online code editors for free you can use (Replit, Online Python, W3 schools, etc...)

from random import randintprint("Welcome to Dice Rolling Game")print("Intiating Game")guess = input("Your Guess: ")die1 = randint(1,6)die2 = randint(1,6)total = die1 + die2if total == guess:    print("Correct!")else:    print("Incorrect!")    print(total)

Original Link: https://dev.to/roman_22c01bcfb71/create-basic-dice-rolling-game-in-python-420f

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To