SHARE
TWEET

Untitled

a guest Jul 21st, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import datetime
  2. import json
  3.  
  4. name = input("Let's play a guessing game. First enter your name: ")
  5. secret = 12
  6. attempts = 0
  7. with open("score_list.txt", "r") as score_file:
  8.     score_list = json.loads(score_file.read())
  9.  
  10. for score_file in score_list:
  11.     print(str(score_file["attempts"]) + " attempts, date: " + score_file.get("date"))
  12.  
  13. wrong_guesses = [],
  14. while True:
  15.     guess = int(input("Guess the secret number (between 1 and 100): "))
  16.     attempts = attempts + 1
  17.     wrong_guesses = attempts - 1
  18.     if guess == secret:
  19.         score_list.append({"player name": name, "attempts": attempts, "date": str(datetime.datetime.now()), "secret number": secret, "wrong_guesses": wrong_guesses })
  20.         print("You've guessed it  congratulations! It's number " + str(secret))
  21.         print("Attempts needed: " + str(attempts))
  22.         break
  23.     elif guess != secret:
  24.         print("Wrong number. Try again")
  25.  
  26. with open("score_list.txt", "w") as dat:
  27.     dat.write(json.dumps(score_list))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top