Some suggestions:
Pulling out functions or classes w/methods will allow you to reduce the amount of nesting in the code, and at the same time will package the code into easily understood functionally and semantically separate parts. This is incredibly important in larger programs, but can also highlight ways in which existing shorter programs can be structured for maintainability. For example, a
Playerclass could have ascorefield and aGameclass could have aroundsfield and arollmethod:MAX_ROUNDS = 5 class Player: def __init__(self): self.score = 0 def win(self): self.score += 10 def lose(self): self.score -= 5 class Game: rounds = 0 player_1 = Player() player_2 = Player() def play(): if self.rounds >= MAX_ROUNDS: … else: … self.rounds += 1Dependency injection of
randintandinputwould allow this code to be unit tested.If someone wants to play this a lot they would probably not appreciate the “dramatic pause” before showing the result of the rolls. I would simply get rid of the
sleeps.