vendredi 27 mars 2020

How do I keep the score in my guessing game?

I want to reward the player with a point when they guess the random number correctly and retain that value when they decide to play again by recalling the game function and not have it reset back to zero. Thanks in advance.

# 'Guess Again' by Brent K Kohler AKA deusopus
def guess_again():
  import random
  n = random.randrange(1,6)
  t = 0
  s = 0
  print("Guess a number between 1 and 5")
  while True:
    i = int(input("Guess: "))
    if i == n:
      print("You Win!")
      print(f"The number was {n}")
      s += 1
      print(f"Score: {s}")
      break
    if t == 2:
      print("Sorry...")
      print("Game Over")
      print(f"The number was {n}")
      break
    else:
      print("Wrong")
      print("Try again")
      t += 1
  return
guess_again()

while True:
  x = input("Press 'enter' to play again. Type 'exit' to quit: ")
  if x.lower() != "exit":
    guess_again()
  else:
    print("Thanks for playing!")
    break



Aucun commentaire:

Enregistrer un commentaire