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 randint
print("Welcome to Dice Rolling Game")
print("Intiating Game")
guess = input("Your Guess: ")
die1 = randint(1,6)
die2 = randint(1,6)
total = die1 + die2
if total == guess:
print("Correct!")
else:
print("Incorrect!")
print(total)
Top comments (0)