Number Guessing Game - Build a Number Guessing Game

Tell us what’s happening:

I did all of the code needed and tested it and there is always the same two tests that are not working since I’ve checked thousands of times and I haven’t found anything wrong with my code!

Tests that are failing:

If that username has been used before, it should print Welcome back, <username>! You have played <games_played> games, and your best game took <best_game> guesses. , with <username> being a users name from the database, <games_played> being the total number of games that user has played, and <best_game> being the fewest number of guesses it took that user to win the game.

&

When the secret number is guessed, your script should print You guessed it in <number_of_guesses> tries. The secret number was <secret_number>. Nice job! and finish running

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0

Challenge Information:

Number Guessing Game - Build a Number Guessing Game

My code so far:

For the capture of the name of the user:

# PSQL variable for database queries
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"

# Prompt for username
echo "Enter your username:"
read USERNAME

# Ensure username is less than or equal to 22 characters
if [[ ${#USERNAME} -gt 22 ]]; then
  echo "Error: Username cannot be longer than 22 characters."
  exit 1
fi

# Check if username exists in the database
USER_INFO=$($PSQL "SELECT games_played, best_game FROM users WHERE username='$USERNAME'")

# If user exists
if [[ -n "$USER_INFO" ]]; then
  # Parse user information
  IFS='|' read GAMES_PLAYED BEST_GAME <<< "$USER_INFO"
  
  # Greet returning user
  echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
else
  # If user does not exist, insert them into the database
  INSERT_USER_RESULT=$($PSQL "INSERT INTO users(username) VALUES('$USERNAME')")
  echo "Welcome, $USERNAME! It looks like this is your first time here."
fi

For the evaluation of the guessed number and the printing of the successful guess:

# Generate a random number between 1 and 1000
SECRET_NUMBER=$(( RANDOM % 1000 + 1 ))

# Output to indicate the game has started
echo "Guess the secret number between 1 and 1000:"

# Initialize guess counter
GUESS_COUNT=0
GUESS=0

# Function to check if input is an integer
is_integer() {
  [[ "$1" =~ ^[0-9]+$ ]]
}

# Loop to take guesses
while [[ $GUESS -ne $SECRET_NUMBER ]]; do
  GUESS_COUNT=$(( GUESS_COUNT + 1 ))
  echo "Enter your guess:"

  # Read user input
  read GUESS

  # Check if input is an integer
  if ! is_integer "$GUESS"; then
    echo "That is not an integer, guess again:"
    continue
  fi

  # Compare the guess with the secret number
  if [[ $GUESS -lt $SECRET_NUMBER ]]; then
    echo "It's higher than that, guess again:"
  elif [[ $GUESS -gt $SECRET_NUMBER ]]; then
    echo "It's lower than that, guess again:"
  fi
done

# Once the user guesses correctly, update the database
echo "You guessed it in $GUESS_COUNT tries. The secret number was $SECRET_NUMBER. Nice job!"

Hi, @jujucastro2703

Nice to see you working on your tasks! Just a little thing…

I’ve modified your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Additionally, please restrain of posting the whole answer? Just try to focus on the specifics. That help us to find the issues as well as allow other students to try to solve the problem by themselves.

What are the outputs, @jujucastro2703 ?

##The output when it’s a new username is:
camper: /number_guessing_game$ ./number_guess.sh
Enter your username:
me
Welcome, me! It looks like this is your first time here.
Guess the secret number between 1 and 1000:
Enter your guess:
500
It’s higher than that, guess again:
Enter your guess:
750
It’s higher than that, guess again:
Enter your guess:
900
It’s lower than that, guess again:
Enter your guess:
800
It’s lower than that, guess again:
Enter your guess:
760
It’s lower than that, guess again:
Enter your guess:
755
It’s lower than that, guess again:
Enter your guess:
751
It’s higher than that, guess again:
Enter your guess:
753
You guessed it in 8 tries. The secret number was 753. Nice job!

##And the output when I put a already used username is:

Enter your username:
me
Welcome back, me! You have played 1 games, and your best game took 8 guesses.
Guess the secret number between 1 and 1000:
Enter your guess:
500
It’s lower than that, guess again:
Enter your guess:
300
It’s higher than that, guess again:
Enter your guess:
400
It’s higher than that, guess again:
Enter your guess:
450
It’s higher than that, guess again:
Enter your guess:
460
It’s higher than that, guess again:
Enter your guess:
480
It’s lower than that, guess again:
Enter your guess:
470
It’s higher than that, guess again:
Enter your guess:
475
It’s lower than that, guess again:
Enter your guess:
472
It’s higher than that, guess again:
Enter your guess:
473
You guessed it in 10 tries. The secret number was 473. Nice job!

I have been testing my code then I decided to check the users table and there were a lot of saved players that I didn’t play as so the system must have added them so I deleted them and modified my code a little bit then I tested it again and looked at the users table and it looked good all the info was correct but then I tryed it a second time and random users were added I deleted them and changed the code a little bit then I tryed twice again no bad info but when I tryed doing with another username and do a little bit but then quit to see if it would cause trouble it added some new random users to the table and now I’m trying to fix it

I already got the table fixed but the same two tests are still failing

Thanks, @jujucastro2703 !

Can you please keep the right format? It is more difficult to distinguish what it is code / output from message when no format is applied.

@jujucastro2703

How are you getting this value?

<best_game> being the fewest number of guesses it took that user to win the game

If you play more than 3-4 times, do you get the best of them as in the request?

These are some of my plays that I did and everything looks like it was running correctly:
Enter your username: Julia Welcome, Julia! It looks like this is your first time here. Guess the secret number between 1 and 1000: Enter your guess: 500 It's higher than that, guess again: Enter your guess: 700 It's higher than that, guess again: Enter your guess: 800 It's higher than that, guess again: Enter your guess: 900 It's higher than that, guess again: Enter your guess: 1000 It's lower than that, guess again: Enter your guess: 950 It's lower than that, guess again: Enter your guess: 910 It's higher than that, guess again: Enter your guess: 920 It's higher than that, guess again: Enter your guess: 930 It's higher than that, guess again: Enter your guess: 940 It's higher than that, guess again: Enter your guess: 945 It's higher than that, guess again: Enter your guess: 949 It's lower than that, guess again: Enter your guess: 947 You guessed it in 13 tries. The secret number was 947. Nice job! camper: /number_guessing_game$ ./number_guess.sh Enter your username: Julia Welcome, Julia! It looks like this is your first time here. Guess the secret number between 1 and 1000: Enter your guess: 500 It's lower than that, guess again: Enter your guess: 300 It's higher than that, guess again: Enter your guess: 400 It's higher than that, guess again: Enter your guess: 450 It's higher than that, guess again: Enter your guess: 470 It's lower than that, guess again: Enter your guess: 460 It's higher than that, guess again: Enter your guess: 465 It's lower than that, guess again: Enter your guess: 463 It's lower than that, guess again: Enter your guess: 462 It's lower than that, guess again: Enter your guess: 461 You guessed it in 10 guesses. Well done! Your statistics have been updated. You have now played 1 games, and your best game took 10 guesses. camper: /number_guessing_game$ ./number_guess.sh Enter your username: Julia Welcome back, Julia! You have played 1 games, and your best game took 10 guesses. Guess the secret number between 1 and 1000: Enter your guess: 500 It's higher than that, guess again: Enter your guess: 700 It's lower than that, guess again: Enter your guess: 600 It's lower than that, guess again: Enter your guess: 550 It's lower than that, guess again: Enter your guess: 520 It's lower than that, guess again: Enter your guess: 510 It's lower than that, guess again: Enter your guess: 501 It's higher than that, guess again: Enter your guess: 505 It's higher than that, guess again: Enter your guess: 507 It's higher than that, guess again: Enter your guess: 508 You guessed it in 10 guesses. Well done! Your statistics have been updated. You have now played 2 games, and your best game took 10 guesses. camper: /number_guessing_game$ ./number_guess.sh Enter your username: Julia Welcome back, Julia! You have played 2 games, and your best game took 10 guesses. Guess the secret number between 1 and 1000: Enter your guess: 500 It's lower than that, guess again: Enter your guess: 200 It's higher than that, guess again: Enter your guess: 300 It's higher than that, guess again: Enter your guess: 400 It's higher than that, guess again: Enter your guess: 450 It's higher than that, guess again: Enter your guess: 480 It's higher than that, guess again: Enter your guess: 490 It's higher than that, guess again: Enter your guess: 500 It's lower than that, guess again: Enter your guess: 499 You guessed it in 9 guesses. Well done! Your statistics have been updated. You have now played 3 games, and your best game took 9 guesses. camper: /number_guessing_gam camper: /number_guessing_game$ ./number_guess.sh Enter your username: 500 Welcome, 500! It looks like this is your first time here. Guess the secret number between 1 and 1000: Enter your guess: ^C camper: /number_guessing_game$ ./number_guess.sh Enter your username: Julia Welcome back, Julia! You have played 3 games, and your best game took 9 guesses. Guess the secret number between 1 and 1000: Enter your guess: 500 It's higher than that, guess again: Enter your guess: 700 It's lower than that, guess again: Enter your guess: 600 It's higher than that, guess again: Enter your guess: 650 It's higher than that, guess again: Enter your guess: 680 It's higher than that, guess again: Enter your guess: 690 It's higher than that, guess again: Enter your guess: 699 You guessed it in 7 guesses. Well done! Your statistics have been updated. You have now played 4 games, and your best game took 7 guesses. camper: /number_guessing_game$ ./number_guess.sh Enter your username: Patrick Welcome, Patrick! It looks like this is your first time here. Guess the secret number between 1 and 1000: Enter your guess: 500 It's higher than that, guess again: Enter your guess: 700 It's higher than that, guess again: Enter your guess: 800 It's higher than that, guess again: Enter your guess: 900 It's lower than that, guess again: Enter your guess: 890 It's lower than that, guess again: Enter your guess: 850 It's lower than that, guess again: Enter your guess: 830 It's lower than that, guess again: Enter your guess: 810 It's higher than that, guess again: Enter your guess: 820 It's lower than that, guess again: Enter your guess: 815 It's lower than that, guess again: Enter your guess: 811 You guessed it in 11 guesses. Well done! Your statistics have been updated. You have now played 1 games, and your best game took 11 guesses.

I miraculously did the first one that wasn’t working but the other one still won’t work!
30.12.2024_14.03.13_REC

after I commited the changes I clicked run and the one that was finally right went to wrong at first I was very confused I looked everywhere to find answers then I clicked again and both the ones that wouldn’t become correct, became correct and I finished the test thanks for your help!

1 Like