guess.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #guess.go converted to python code
  2. import random
  3. import sys
  4. print("LET'S PLAY A GUESSING GAME! YOU WILL HAVE 10 GUESSES to GUESS A RANDOM NUMBER FROM 1 TO 100!\n")
  5. playAgain = True
  6. while playAgain:
  7. targetNum = random.randint(1,100)
  8. success = True
  9. for guesses in range(1,11):
  10. userguess = input("Enter guess #%d : " %(guesses))
  11. try:
  12. guess = int(userguess)
  13. except:
  14. sys.exit("Your guess "+userguess+" is invalid!")
  15. if guess < targetNum:
  16. print("OOPS! Your guess %d is lesser than the random number!\n" %(guess))
  17. success = False
  18. elif guess > targetNum:
  19. print("OOPS! Your guess %d is greater than the random number!\n" %(guess))
  20. success = False
  21. elif guess == targetNum:
  22. print("Congratulations! You guessed the number!")
  23. success = True
  24. break
  25. if not success:
  26. print("Sorry, Game over! The random number is %d" %(targetNum))
  27. repeat = ''
  28. while repeat != 'Y' and repeat != 'N':
  29. repeat = input("Do you want to play again ? (Y/N) : ")
  30. if repeat == 'N':
  31. playAgain = False
  32. print("Ok, thank you for playing. Goodbye!")
  33. if repeat == 'Y':
  34. playAgain = True
  35. print("Ok, here we go again!")