SHARE
TWEET

Untitled

a guest Jul 18th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #store date
  2. #store the name of the player
  3. #store secret number in each game.
  4. #Show wrong attempts
  5.  
  6.  
  7. import random
  8. import json
  9. import datetime
  10.  
  11. current_time = datetime.datetime.now()
  12.  
  13. secret = random.randint (1, 30)
  14.  
  15. attempts = 0
  16.  
  17. wrong_attempts = attempts - 1
  18.  
  19. score_list = 0
  20.  
  21. name = input("Tell us your name: ")
  22.  
  23. with open("score_list.txt", "r") as score_file:
  24.     score_list = json.loads(score_file.read())
  25.     print("Top scores: " + str (score_list) + "\t")
  26.  
  27. for score_dict in score_list:
  28.     print(str(score_dict["attempts"]) + " attempts, date: " + score_dict.get("date"))
  29.  
  30. while True:
  31.  
  32.  
  33.     guess = int ( input ("Guess a random number between 1 and 30"))
  34.     attempts = attempts + 1
  35.  
  36.     if guess == secret:
  37.         score_list.append({"name": name, "attempts": attempts, "date": str(datetime.datetime.now()), "secret_number": secret, "wrong_attempts": attempts -1 })       #Let's also store the name of the player and the secret number in each game.
  38.  
  39.         with open("score_list.txt", "w") as score_file:
  40.             score_file.write(json.dumps(score_list))
  41.  
  42.         print ("You've guessed it - congratulations! It's number " + str(secret))
  43.         print ("Attempts needed: " + str (attempts))
  44.         print ("Wrong attempts : " + str (attempts -1))
  45.         break
  46.  
  47.  
  48.  
  49.  
  50.     elif guess > secret:
  51.         print( "Guess lower. Try something smaller")
  52.     elif guess < secret:
  53.         print("Guess higher. Try something bigger")
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