Skip to content

Instantly share code, notes, and snippets.

@bugkiller78
Created November 2, 2020 20:06
Show Gist options
  • Save bugkiller78/385b2a0b6914cd3b2840b823d0bf2d7b to your computer and use it in GitHub Desktop.
Save bugkiller78/385b2a0b6914cd3b2840b823d0bf2d7b to your computer and use it in GitHub Desktop.
import datetime
import json
import random
secret = random.randint(1,30)
attempts = 0
current_time = datetime.datetime.now()
wrong_attempts = 0
print('Current date and time ' + str(current_time))
with open('score_list.txt' , 'r') as score_file: # odpre datoteko score list txt
score_list = json.loads(score_file.read())
with open('wrong_guess.txt', 'r') as wrong_file: # odbre datoteko z napačnimi poizkusi
wrong_guess = json.loads(wrong_file.read())
print('Best score list: ')
score_list.sort(key=lambda item: item.get("attempts")) #sortira glede na poizkuse od najmanjše proti najvišji vrednosti
for score_dict in score_list:
print('User name ' + score_dict["ime igralca"] + ' in ' + str(score_dict["attempts"]) + " attempts, date: " + score_dict.get("date"))
ime = str.upper(input('Please enter your name: '))
while True:
guess = int(input(ime + ' Please guess your secret number between 1 and 30: '))
attempts += 1
if guess > 30 or guess < 1:
print('Guessed number is invalid, please insert a valid number!')
elif guess == secret:
score_list.append({"ime igralca": ime, "secret_number": secret, "attempts":str(attempts), "date": str(datetime.datetime.now())})
with open('score_list.txt', 'w') as score_file:
score_file.write(json.dumps(score_list)) # obvezno v datoteki score_list.txt začeti z oklepaji []
print('Congratulations, you guessed the right number ' + str(secret) + ' in ' + str(attempts) + ' attempts!')
break
elif guess < secret:
wrong_attempts += 1 # štetje napačnih odgovorov
wrong_guess.append({'wrong_attempt': wrong_attempts}) #zapis napačnih odgovorov v novo txt datoteko
with open('wrong_guess.txt', 'w') as wrong_file:
wrong_file.write(json.dumps(wrong_guess))
print('Sorry,the number ' + str(guess) + ' is not coorect, please try with something bigger!')
print('You have '+ str(wrong_attempts) +' wrong guesses!')
elif guess > secret:
wrong_attempts += 1
wrong_guess.append({'wrong_guess': wrong_attempts})
with open('wrong_guess.txt', 'w') as wrong_file:
wrong_file.write(json.dumps(wrong_guess))
print('Sorry,the number ' + str(guess) + ' is not coorect, please try with something smaller!')
print('You have ' + str(wrong_attempts) + ' wrong guesses!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment