Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

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 below 0.

I've tried some of the answers to the questions on this site but they didn't work.

I want to make sure score can't go below 0.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 + 10
      print("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 + 10
      print("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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...