Skip to content

Instantly share code, notes, and snippets.

@Chaitran77
Created July 27, 2017 08:23
Show Gist options
  • Save Chaitran77/52f4b036c68a640528fc6d7208c23adc to your computer and use it in GitHub Desktop.
Save Chaitran77/52f4b036c68a640528fc6d7208c23adc to your computer and use it in GitHub Desktop.
Guess the number game created by chaitran77 - https://repl.it/I6bK/315
import time
import random
import datetime
# number = random.randint(0,100) <-- this is commented out because it will only generate a random number 1'ce
# every time loop() is run, number needs to be redifined. Better to define in loop(). Can't globalise number.
prompt = ">>>"
gu_num = 0
round = 1 # this is fine because built-in round will not be used
counter = 10
highscores = []
regeneration = False
number = random.randint(1, 100)
"""
def guess_input():
for i in range(20):
guess = input("What is your guess?")
guess_evaluate()
"""
def guess_evaluate():
global gu_num
print("You are on guess number: " + str(gu_num))
guess = input("What is your guess? " + prompt)
try:
if (int(guess) > 100) or (int(guess) < 0):
print("Your number was out of range. Remember, you can only choose numbers from 1 to 100. Please try again:")
elif int(guess) < (number):
print("=====================")
print("====> Higher... <====")
print("=====================")
#input() # so the user doesn't forget to read 'Higher'
#global gu_num
gu_num += 1
#print(str(gu_num))
elif int(guess) > int(number):
print("====================")
print("====> Lower... <====")
print("====================")
#input() # so the user doesn't forget to read 'Lower'
#global gu_num
gu_num += 1
else:
print("Correctomundo! My number was " + str(number) + ".")
file_operations()
except ValueError:
print("Invalid input. Please try again:")
def file_operations():
percent = gu_num * 10
score = 100 - percent
current_score = {round: (score)}
highscores.append(score)
print("Your score in this round is " + str(current_score[round]))
print("Your high score, so far, has been " + str(max(highscores)))
save = input("\nWould you like your score to be saved in a plain txt file? y/n " + prompt)
if (save.upper() == "Y") or (save.upper() == "YES"):
try:
file = open("guess-the-number.txt", "a+") #add the date
info = ["\n---------------------------------------------ROUND ", str(round), "-------------------------------------------\n","\n\n\t\t\t\t Welcome to your personalised\n \t\t\t\t\tGuess The Number\n \t\t\t\t\t Details document\n\n", "On the ", str(datetime.date.today()), " at ", str(datetime.datetime.now().time()), "\n", str(name1).title(), " ", str(name2).title(), " guessed the number ", str(number), " in ", str(gu_num), "/10 ", "attempts\n", "That means that your score is ", str(score), " because you were ", str(percent), "% through the game when you guessed the correct number.", "\n\nThank you for playing Guess The Number and I hope to see you again soon!\nThis game was created and devoloped by Kiran Patel (me), age 12. If you liked this game or want to talk to me about -->anything<--, please do email me at [email protected]. It'll make me happy!", "\n--------------------------------------------END OF ROUND ", str(round), "-------------------------------------------\n"]
#print(info)
file.writelines(info) # add number of attempts, date, credits, thank you,
print("File saved successfully!")
except IOError:
print("There was an error handling your file. Sorry about that. This probably means that there is something wrong with your hard disk. Check to see if there is avaliable memory and that there are no corrupted sectors. The storage device could also be in read-only mode.")
play_again()
elif (save.upper() == "N") or (save.upper() == "NO"):
print("Ok then, your score has not been saved in a plain .txt file.")
play_again()
else:
print("Sorry, I don't understand. Please try again:")
file_operations()
read = input("Would you like to see what the file looks like? y/n " + prompt)
if (read.upper() == "Y") or (read.upper() == "YES"):
file = open("guess-the-number.txt", "r")
try:
print("The contents of your file are:\n" + file.read())
file.close()
play_again()
except IOError:
print("There was an error handling your file. Sorry about that. This probably means that there is something wrong with your hard disk. Check to see if there is avaliable memory and that there are no corrupted sectors. ")
play_again()
elif (read.upper() == "N") or (read.upper() == "NO"):
print("\nOk then, I'll let you check it out for yourself.")
play_again()
else:
print("Sorry, I don't understand. Please try again:")
file_operations()
def play_again():
again = input("\nWould you like to play again? y/n " + prompt)
if (again.upper() == "Y") or (again.upper() == "YES"):
global gu_num
percent = gu_num * 10
score = 100 - percent
highscores = [{round: (score)}]
current_score = {round: (score)}
highscores.append(current_score)
print("Lets see if you can beat your score of " + str(current_score[round]) + ".\nHere we go!")
gu_num = 0
regeneration = True
loop()
elif (again.upper() == "N") or (again.upper() == "NO"):
print("Ok then.\nThank you for playing Guess The Number and I hope to see you again soon!\nThis game was created and devoloped by Kiran Patel (me), age 12. If you liked this game or want to talk to me about -->anything<--, please do email me at [email protected]. It'll make me happy! Thank you for playing Guess the number with me.\n\n program was developed by Kiran Patel in 2017 ad.")
input("\n\nPress the enter key to exit")
quit()
else:
print("Sorry, I don't understand. Please try again:")
play_again()
while regeneration:
print("Regenerating random number...")
number = random.randint(1, 100)
time.sleep(1)
print("Number regenerated.")
def loop():
regeneration = False
while gu_num != 10:# not anymore!
#for i in range(10):# this is the problem
guess_evaluate()
print("Oh no, you ran out of gusses :-(")
file_operations()
"""
round += 1
percent = gu_num * 5
score = 100 - percent
highscores = [{round: (score)}]
current_score = {round: (score)}
highscores.append(current_score)
"""
print("Welcome to the classical version of the Guess The Number game!\nProgrammed purely in Python 3.x, this code will generate a random number from 1 to 100 for you to guess in 10 attempts.\nWhen the '>>>' prompt is displayed, press enter once you have finished typing. Good luck! (Note: Some graphics will only look correct if this program is displayed in a mono-spaced font.)")
input("\nPress the enter key to continue")
name1 = input("What is your first name? " + prompt)
name2 = input("What is your surname? " + prompt)
print("\n\t\t\t\t@@@@@@@@@@@@@@\n\t\t\t\t@>>>>>><<<<<<@\n\t\t\t\t@>~~~~~~~~~~<@\n\t\t\t\t@>~ round " + str(round) + "~<@\n\t\t\t\t@>~~~~~~~~~~<@\n\t\t\t\t@>>>>>><<<<<<@\n\t\t\t\t@@@@@@@@@@@@@@\n")
loop()
#https://gist.github.com/anonymous/9cba6444e6b84aa013fd765937610de7
#https://gist.github.com/anonymous/c6671134ea8381d83e15037b89cd0dc5
#https://gist.github.com/anonymous/36777923711fd8bf13618d8cb03d3232
#https://gist.github.com/anonymous/3b73b7acd901b731c11bd913edd9fa5e
#https://gist.github.com/anonymous/c4658f403d63816b300e848447697bfa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment