Guest User

Untitled

a guest
Feb 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. from random import randint
  2.  
  3. guesses = 10
  4. def numGuess():
  5. #calls guess function with a randomly generated number, and specified guesses
  6. return numGuessHelper(randint(1, 20), guesses)
  7.  
  8. def inputHelper(prompt):
  9. while True:
  10. try:
  11. value = int(input(prompt))
  12. except ValueError:
  13. print("Enter only integers please.")
  14. continue
  15. if value < 0 or value > 20:
  16. print("Your guess was outside the possible bounds, try again.")
  17. else:
  18. break
  19. return value
  20.  
  21. def numGuessHelper(n, g):
  22. #if there are guesses left, then true
  23. if g > 0:
  24. #returns the guess if it isn't an error and is a viable answer
  25. temp = inputHelper("Guess a number between 1 and 20: ")
  26. #returns if the guess is equal to the random number
  27. if temp == n:
  28. return "You have guessed the correct answer."
  29. #otherwise removes a guess and continues the function
  30. else:
  31. print("Incorrect. You have", g-1, "more guesses.")
  32. return numGuessHelper(n, g-1)
  33. #if there are no guesses left then the function ends
  34. else:
  35. return "You have used up all your guesses."
  36.  
  37. print(numGuess())
Add Comment
Please, Sign In to add comment