骰子游戏一旦达到0,停止号码就会减少-Python

时间:2019-05-09 10:52:49

标签: python

我在python上有一个骰子游戏,您掷骰子两次并添加分数,当您获得奇数时,分数降低5。我需要确保分数不能低于0。 / p>

我已经尝试过本网站上问题的一些答案,但没有用。

我要确保得分不能低于0。

1 个答案:

答案 0 :(得分:3)

您可以像这样使用max():

playerOnePoints = max(0, playerOnePoints - 5)
playerTwoPoints = max(0, playerTwoPoints - 5)

编辑: 这可以回答您的问题,但是您的代码已完全损坏,下面是有效的代码:

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!")

尚不清楚您是否希望总分是骰子的总和加上10还是减去5(如果总和是成对或减损,那么我做到了)。