Skip to content

Instantly share code, notes, and snippets.

@FaisalFehad
Last active December 25, 2016 19:54
Show Gist options
  • Save FaisalFehad/84ea79a732e42398f6f75113df7604e5 to your computer and use it in GitHub Desktop.
Save FaisalFehad/84ea79a732e42398f6f75113df7604e5 to your computer and use it in GitHub Desktop.
Second Python code. Guess number game
def game():
# import random
import random
# generate a random number
secrit_num = random.randint(1, 10)
# count the attepts to guess
attempts = 0
# run the loop until uses the 5 attepts
while attempts <= 4:
# ask to guess the number
user_input = int(input("Guess a number between 1 and 10? "))
# add 1 to the attempts variable
attempts += 1
# print the attepts left out of 5
print("You have {} attempts left".format(5 - attempts))
# check the answer
if user_input == secrit_num:
print("That's Right! {} is what I was thinking of!!!".format(secrit_num))
# break the loop if was correct
break
# can try guess again with hints
else:
if secrit_num >= 5:
print("That's not it! Its 5 or higher")
elif secrit_num <= 5:
print("Thats not it! its 5 or lower")
# when the attepts end
print("You have used your attepts. The number I was thinking of {}".format(secrit_num))
# Start the game/again
play_again = input("The game wil think of a number and you would need to guess it. You would have 5 attepts to do so. Would you like to start Y/n ")
if play_again == "Y":
game()
else:
print("Bye!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment