Here’s an example of how to create a simple Python dice game:
import random
def roll_dice():
return random.randint(1, 6)
def play_game():
print("Welcome to the dice game!")
print("You will roll two dice and try to get the highest score.")
roll1 = roll_dice()
roll2 = roll_dice()
total = roll1 + roll2
print(f"You rolled a {roll1} and a {roll2}, for a total of {total}.")
if total >= 10:
print("You win!")
else:
print("Sorry, you lose.")
play_again = input("Do you want to play again? (y/n): ")
if play_again.lower() == "y":
play_game()
else:
print("Thanks for playing!")
In this example, we define two functions: roll_dice()
which returns a random integer between 1 and 6, and play_game()
which rolls two dice, calculates their total, and determines whether the player wins or loses based on the total.
We then call play_game()
to start the game, and allow the player to play again by recursively calling play_game()
if they choose to do so.
Note that this is a very simple example, and you could add more complexity to the game by adding more dice, allowing the player to place bets, or implementing different winning conditions.
+ There are no comments
Add yours