Advertisement
Guest User

guese_game.py

a guest
Jan 15th, 2021
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. '''
  2. -----------Guess Game----------
  3. Author:Divyansh Chourey
  4. Source code:
  5. '''
  6.  
  7. import random
  8. #Generating the random number
  9. randNum=random.randint(1,100)
  10. #print(randNum)
  11.  
  12. print("$\tWelcome to Guess Game\t$")
  13. print("Guess any number from 1 to 100\n")
  14.  
  15. #Opening the high_score file to print highscore
  16. with open ('high_score.txt','r') as f:
  17.     highscore=f.read()
  18.     highscore=int(highscore)
  19.     # TODO: write code...
  20. print(f"The High Score is {highscore}\n")
  21.  
  22. #Some of the parameters used
  23. active=True
  24. guess=0
  25.  
  26. #While the user is active - continue
  27. while active:
  28.     #Taking the input from user.
  29.     user=int(input("Enter the number: "))
  30.     #Algorithm to varify the results.
  31.     if randNum == user:
  32.         print("You guessed it right.\n")
  33.         active=False
  34.     else:
  35.         if randNum>user:
  36.             print("Wrong guess. Guess larger number.\n")
  37.         elif randNum<user:
  38.             print("Wrong guess. Guess smaller number.\n")
  39.     guess+=1
  40.  
  41. #Displaying the number of times user guessed
  42. print(f"You guess the number in {guess} guesses")
  43.  
  44. if highscore==guess:
  45.     print("You have just reached the record.")
  46.  
  47. #Writing the score if it is higher than highscore
  48. elif (highscore>=guess):
  49.     with open('high_score.txt', 'w') as f:
  50.         guess=str(guess)
  51.         f.write(guess)
  52.         print("You broke the record.")
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement