Skip to content

Instantly share code, notes, and snippets.

@sumon328
Created August 19, 2020 15:34
Show Gist options
  • Save sumon328/7c9cc2b1c118510ffa49e37d76fb55d0 to your computer and use it in GitHub Desktop.
Save sumon328/7c9cc2b1c118510ffa49e37d76fb55d0 to your computer and use it in GitHub Desktop.
# Generate a random number between 1 and 9 (including 1 and 9). Ask the user to guess the number, then tell them whether they guessed too low, too high, or exactly right. (Hint: remember to use the user input lessons from the very first exercise)
# # Extras:
#
# Keep the game going until the user types “exit”
# Keep track of how many guesses the user has taken, and when the game ends, print this out.
# In[209]:
# generate a random number
import random
rNumber = random.randint(1, 9)
count = 0
while (True):
# asking the user to guess the number
guessNumber = input("Please, type your guess number or 'exit' to stop the game: ")
# verifying if the user wants to stop
if guessNumber == 'exit':
break
# validating the user guess
elif rNumber == int(guessNumber):
count += 1
print("Congratulations! You have typed {} and the random number is {}".format(guessNumber, rNumber))
break
elif int(guessNumber) > rNumber:
count += 1
print("Error! Your guess number {} is too high, please try a smaller number".format(guessNumber))
else:
count += 1
print("Error! Your guess number {} is too low, please try a higher number".format(guessNumber))
# verifying how many guesses the user typed and the random number
print('You have typed {} guesses '.format(count))
print('The random number was {}'.format(rNumber))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment