Advertisement
Guest User

python_memory_game

a guest
Oct 21st, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | None | 0 0
  1. import random
  2. import time
  3. import os
  4. import sys
  5. import timeit
  6. import json
  7.  
  8. with open("stats.json") as statistics_file:
  9.     statistics = json.load(statistics_file)['game_review']
  10.     for p in statistics:
  11.         print(p['name'], "|", p['time'], "|", p['level'], "\n")
  12.  
  13. print("Welcome to my memory trainer, let's start. You will see a number, try to remember it.\nIf you will write the number correctly, you can go to next level.\nIf you won't, you will have to try again from start.")
  14. time.sleep(1)
  15. level = 0
  16. max_level = 0
  17. show_sec = 1
  18. wait_sec = 1
  19. stopwatch = 0
  20. end_stopwatch = 0
  21. skip_time = input("Do You want to skip waiting time for remembering numbers with Enter? Yes/No\n")
  22. your_name = input("What's your name? Please use the same name that you've used before if you're using the program again.\n")
  23. while True:
  24.     random_number = (random.randint(10**level, 10**(level+1)-1))
  25.     print(random_number)
  26.     stopwatch += time.time()
  27.     if skip_time == ("Yes" or "yes"):
  28.         input("Press Enter to continue and then wait for next command")
  29.     elif  skip_time == "No" or "no":
  30.         time.sleep(show_sec)
  31.     end_stopwatch += time.time()
  32.     os.system("clear")
  33.     time.sleep(wait_sec)
  34.     input_number = int(input("Now write here the number you saw. \n"))
  35.     time_result = end_stopwatch-stopwatch
  36.     if input_number == random_number and level < max_level:
  37.         print("Good job, your answer is correct, let's try something harder.\n")
  38.         level = level + 1
  39.         show_sec = show_sec + 0.5
  40.         wait_sec = wait_sec + 0.3
  41.         time.sleep(1)
  42.         continue
  43.     elif level == max_level:
  44.         print("Good job, that's it for today, we don't have more levels for you right now.\nIt took you", time_result, "seconds to beat the game")
  45.         with open("stat.json", "r") as stat_read:
  46.             reading = json.loads(stat_read.read())
  47.             reading["game_review"] = {
  48.                 "name" : your_name,
  49.                 "time" : round(time_result, 2),
  50.                 "level" : level+1
  51.             }
  52.  
  53.         with open("stat.json", "a") as stats:
  54.             json.dump(reading, stats)
  55.                
  56.         break
  57.     else:
  58.         print("Sorry, that is not right, you can try it again from the beginnng.")
  59.         level = 0
  60.         show_sec = 1
  61.         wait_sec = 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement