My Solution
Guess a number from 1 to 10 game.
import random def init_game(): print("Guess a number between 1 and 10. You have 5 chances.") num = random.randint(1,10) run_game(num) def ask_repeat(): while True: repeat = input("Play again? ") if repeat == 'yes' or repeat == 'Yes': init_game() break elif repeat == 'no' or repeat == 'No': print("Thank you for playing!") break else: print("Please answer only with Yes or No") continue def run_game(num): tries = 5 while True: # Should have used: while len(guessed_list) < tries if tries == 0: print("You lose!") ask_repeat() break guess = int(input("Guess number: \n")) if num > guess: tries -= 1 print("Too low! {} chances left".format(tries)) continue elif num < guess: tries -= 1 print("Too high! {} chances left".format(tries)) continue else: print("Yes! The number is {}. You won!".format(num)) ask_repeat() break init_game() print("-"*20 + "End of Game!" + "-"*20)
The Solution
import random rand_num = random.randint(1, 10) guessed_nums = [] allowed_guesses = 5 while len(guessed_nums) < allowed_guesses: guess = input('Guess a number between 1 and 10: ') try: player_num = int(guess) except: print("That's not a whole number!") break if not player_num > 0 or not player_num < 11: print("That number isn't between 1 and 10!") break guessed_nums.append(player_num) if player_num == rand_num: print('You win! My number was {}.'.format(rand_num)) print('It took you {} tries.'.format(len(guessed_nums))) break else: if rand_num > player_num: print('Nope! My number is higher than {}. Guess #{}'.format( player_num, len(guessed_nums))) else: print('Nope! My number is lower than {}. Guess #{}'.format( player_num, len(guessed_nums))) continue if not rand_num in guessed_nums: print('Sorry! My number was {}.'.format(rand_num))