Stop Number Decrease Once 0 Reached On Dice Game - Python
I have a dice game on python where you roll the dice twice and it adds the score, when you get an odd number your score decreases by 5. i need to make sure that the score can't go
Solution 1:
You could use max() like this:
playerOnePoints = max(0, playerOnePoints - 5)
playerTwoPoints = max(0, playerTwoPoints - 5)
EDIT: This answers your question, but your code is totally broken, here a working code:
import time
import random
total_score1 =0
total_score2 =0
rounds =0
def start_round():
global total_score1
global total_score2
global rounds
rounds = rounds +1
# First player rolls dice
number = random.randint(1,6)
number2 = random.randint(1,6)
playerOnePoints = number + number2
print("-------------------------------------------")
print("Round",rounds)
print("-------------------------------------------")
print("Player 1's turn Type 'roll' to roll the dice")
userOneInput = input(">>> ")
if userOneInput =="roll":
time.sleep(1)
print("Player 1's first roll is", number)
print("Player 1's second roll Type 'roll' to roll the dice")
userOneInput = input(">>> ")
if userOneInput =="roll":
time.sleep(1)
print("player 1's second roll is", number2)
if playerOnePoints %2==0:
playerOnePoints = playerOnePoints +10print("Player 1's total is even so + 10 points")
else:
playerOnePoints =max(0, playerOnePoints -5)
print("player 1's total is odd so -5 points")
total_score1 += playerOnePoints
print("-------------------------------------------")
print("Player 1 has", total_score1, "points")
# Second player rolls dice
number = random.randint(1,6)
number2 = random.randint(1,6)
playerTwoPoints = number + number2
print("-------------------------------------------")
print("Player 2's turn Type 'roll' to roll the dice")
userTwoInput = input(">>> ")
if userTwoInput =="roll":
time.sleep(1)
print("Player 2's first roll is", number)
print("Player 2's second roll Type 'roll' to roll the dice")
userTwoInput = input(">>> ")
if userTwoInput =="roll":
time.sleep(1)
print("player 2's second roll is", number2)
if playerTwoPoints %2==0:
playerTwoPoints = playerTwoPoints +10print("Player 2's total is even so + 10 points")
else:
playerTwoPoints =max(0, playerTwoPoints -5)
print("player 2's total is odd so -5 points")
total_score2 += playerTwoPoints
print("-------------------------------------------")
print("Player 2 has", total_score2, "points")
print("-------------------------------------------")
print("Welcome to dice game!")
print("-------------------------------------------")
max_rounds = int(input("Please enter number of rounds: "))
while rounds < max_rounds:
start_round()
print("-------------------------------------------")
print("End of game")
print("-------------------------------------------")
print("Player 1 has", total_score1, "points")
print("Player 2 has", total_score2, "points")
if total_score1 == total_score2:
print("It's a tie!")
else:
print(f"Player {1 if total_score1 > total_score2 else 2} wins!")
It is not clear if you want the total score to be the sum of dices plus 10 or minus 5 either if the sum is pair or impair, but I did it that way.
Post a Comment for "Stop Number Decrease Once 0 Reached On Dice Game - Python"