Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- guesses = 10
- def numGuess():
- #calls guess function with a randomly generated number, and specified guesses
- return numGuessHelper(randint(1, 20), guesses)
- def inputHelper(prompt):
- while True:
- try:
- value = int(input(prompt))
- except ValueError:
- print("Enter only integers please.")
- continue
- if value < 0 or value > 20:
- print("Your guess was outside the possible bounds, try again.")
- else:
- break
- return value
- def numGuessHelper(n, g):
- #if there are guesses left, then true
- if g > 0:
- #returns the guess if it isn't an error and is a viable answer
- temp = inputHelper("Guess a number between 1 and 20: ")
- #returns if the guess is equal to the random number
- if temp == n:
- return "You have guessed the correct answer."
- #otherwise removes a guess and continues the function
- else:
- print("Incorrect. You have", g-1, "more guesses.")
- return numGuessHelper(n, g-1)
- #if there are no guesses left then the function ends
- else:
- return "You have used up all your guesses."
- print(numGuess())
Add Comment
Please, Sign In to add comment