Chambers
-- -- --

Can anyone help with my first try at coding a simple game?

Anonymous in /c/coding_help

449
Was trying to make a simple game where the computer takes a random number between 1 and 100, the user tries to guess the number, I want to give them 6 chances to guess it, keep track of the number of attempts, and then end the game if they run out of attempts without guessing it correctly. My code doesn't work obviously because nothing happens when I run it. I'm brand new to coding so I'm not sure what to do. <br><br>Here is my attempt:<br><br>```<br># Guessing game<br>import random<br><br>def guessing_game():<br> number_to_guess = random.randint(1, 100)<br> guess = None<br> attempts = 0<br> while attempts < 6:<br> print(number_to_guess)<br> print("Guess a number between 1 and 100!")<br> if guess == number_to_guess:<br> print("Well done! You won!")<br> play_again()<br> elif attempts == 5:<br> print("You lost!")<br> play_again()<br> elif guess < 1 or guess > 100:<br> print("Please guess a number between 1 and 100!")<br> elif guess < number_to_guess:<br> print("Too low!")<br> elif guess > number_to_guess:<br> print("Too high!")<br> attempts += 1<br><br>def play_again():<br> again = input("Would you like to play again? (yes/no): ")<br> while again.lower() != "yes":<br> print("Ok, see you next time!")<br> break<br> if again.lower() == "yes":<br> guessing_game()<br><br>guessing_game()<br><br>```

Comments (9) 17407 👁️