Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import random
secret_number = random.randint(1, 100)
print("Hello! What is your name?")
name = input()
print("Hi, " + str(name) + ". I'm thinking of a number between 1 to 100. I'll give you 7 chances to find it.")
chances = 7
while True:
print("Take a guess.")
guess = input()
chances -= 1
too_low = "Your guess is too low. "
too_high = "Your guess is too high. "
chances_left = "Try again. You have " + str(chances) + " chances left. "
one_chance_left = "Try again. You only have 1 chance left! "
out_of_bounds = "Stay within 1-100, please. "
if guess == "debug":
chances += 1
print("Secret number: " + str(secret_number) + ". Number of chances left: " + str(chances))
else:
try:
guess = int(guess)
if guess > 100 or guess < 1:
chances += 1
print(out_of_bounds, end="")
elif chances > 1:
if guess == secret_number:
break
elif guess < secret_number:
print(too_low, end="")
else:
print(too_high, end="")
print(chances_left, end="")
elif chances == 1:
if guess == secret_number:
break
elif guess < secret_number:
print(too_low, end="")
else:
print(too_high, end="")
print(one_chance_left, end="")
else:
break
except ValueError:
print("Try typing only a whole number.")
chances += 1
if guess == secret_number and chances == 6:
print("Wow, " + name + ", you nailed it instantly! My number was " + str(secret_number) + ". Great job!")
elif guess == secret_number and chances >= 1:
chances += 1
print("Rock on, " + name + "! You guessed my number, " + str(secret_number) + ", with " + str(chances) + " chances left!")
elif guess == secret_number:
print("Whew, you guessed my number just in the nick of time, " + name + "! It was " + str(secret_number) + ".")
else:
print("Darn! Sorry to hand it to ya, but my number was " + str(secret_number) + ". Come again?")
@KeronCyst

This comment has been minimized.

Copy link
Owner Author

@KeronCyst KeronCyst commented Jan 12, 2018

I'm wondering about how to consolidate 31-36 and 39-44 which are identical.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment