Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #store date
- #store the name of the player
- #store secret number in each game.
- #Show wrong attempts
- import random
- import json
- import datetime
- current_time = datetime.datetime.now()
- secret = random.randint (1, 30)
- attempts = 0
- wrong_attempts = attempts - 1
- score_list = 0
- name = input("Tell us your name: ")
- with open("score_list.txt", "r") as score_file:
- score_list = json.loads(score_file.read())
- print("Top scores: " + str (score_list) + "\t")
- for score_dict in score_list:
- print(str(score_dict["attempts"]) + " attempts, date: " + score_dict.get("date"))
- while True:
- guess = int ( input ("Guess a random number between 1 and 30"))
- attempts = attempts + 1
- if guess == secret:
- 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.
- with open("score_list.txt", "w") as score_file:
- score_file.write(json.dumps(score_list))
- print ("You've guessed it - congratulations! It's number " + str(secret))
- print ("Attempts needed: " + str (attempts))
- print ("Wrong attempts : " + str (attempts -1))
- break
- elif guess > secret:
- print( "Guess lower. Try something smaller")
- elif guess < secret:
- print("Guess higher. Try something bigger")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement