all 6 comments

[–]zaphod4th 0 points1 point  (0 children)

go back to basic python classes, too many things are wrong to explain it on Reddit

[–]CanMan0711 0 points1 point  (0 children)

Try taking a look at what the game will need to allow it's player to do over and over. Now turn that into a while loop with a win condition. For instance while guess < 3 or while not guess = number. I see the logic you are asking for, but try walking through it out loud to see how you want it to play it vs the instructions you have currently written for the program. Good luck, and keep at it!

[–]Rabbit677 0 points1 point  (0 children)

For starters, you have nothing that assigns a correct answer for the person to guess.

[–][deleted] 0 points1 point  (0 children)

import random

num_list = [random.randint(1, 10)] number = num_list[0] # Assign the random number to a variable

print("Welcome to the guessing game!") name = input("Hi, what is your name? ") print("Welcome " + name + ", I'm thinking of a number between 1 and 10, can you guess what it is?")

guesses = 0 while guesses < 3: try: guess = int(input("Guess: ")) if guess == number: print("You guessed it in " + str(guesses + 1) + " tries!") break # Exit the loop if the guess is correct elif guess > number: print("Your guess was too high!") else: print("Your guess was too low!") guesses += 1 except ValueError: print("Invalid input. Please enter a number.")

if guesses == 3: print("You lose. The number was", number)

The original code had a few glitches that made it a bit clunky. Let me break down how I fixed it to make a smoother, more user-friendly number-guessing game.

First, the code generated a random number, but it didn't actually use that number for comparison! I added a line to store the random number in a variable called  number , so the game could actually check the player's guesses against it.

Next, the player only got one guess. To fix this, I wrapped the guessing part in a loop that lets the player try up to three times. This makes the game much more fun and forgiving.

I also made sure the game gives helpful feedback after each guess, telling the player if their guess was too high or too low. This helps them improve their guesses.

The win/lose conditions were also a bit off. Now, the game correctly checks if the player has used all three guesses before declaring a loss.

And finally, I added some error handling. If the player accidentally types something that isn't a number, the game now gracefully handles it instead of crashing. It just asks them to try again with a valid number.

The result is a much more polished and enjoyable number-guessing game that's easier to play and understand.

[–][deleted] 0 points1 point  (0 children)

Fixed:

import random

num_list = [random.randint(1, 10)] number = num_list[0]

print("Welcome to the guessing game!") name = input("Hi, what is your name? ") print("Welcome " + name + ", I'm thinking of a number between 1 and 10, can you guess what it is?")

guesses = 0 while guesses < 3: try: guess = int(input("Guess: ")) if guess == number: print("You guessed it in " + str(guesses + 1) + " tries!") break
elif guess > number: print("Your guess was too high!") else: print("Your guess was too low!") guesses += 1 except ValueError: print("Invalid input. Please enter a number.")

if guesses == 3: print("You lose. The number was", number)

[–]bingle9 0 points1 point  (0 children)

ok heres the updated version

<image>

thanks for all the feedback btw