Number Guessing Game

Hello, I’m struggling with passing 3 tests:

  1. When you run your script, you should prompt the user for a username with Enter your username: , and take a username as input. Your database should allow usernames of at least 22 characters
  2. 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
  3. 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!

I’ve read that we should specify the column username and I set it VARCHAR(25), but still cannot pass.

#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"

GUESS_LOOP(){
  read GUESS_NUMBER 
  if [[ $GUESS_NUMBER =~ ^[0-9]+$ ]]
  then
    while [[ $THE_NUMBER == "NOT FOUND" ]]
    do   
      ONE_TRY $RANDOM_NUMBER $GUESS_NUMBER
    done
  else
    echo "That is not an integer, guess again:"
    GUESS_LOOP
  fi

}

ONE_TRY(){
  
  if [ $1 -lt $2 ]
  then
  echo "It's lower than that, guess again:"
    ((TRIES++))
    GUESS_LOOP
  
  elif [ $1 -gt $2 ]
  then 
    echo "It's higher than that, guess again:"
    ((TRIES++))  
    GUESS_LOOP
   
  elif [ $1 -eq $2 ]
  then
    echo "You guessed it in $TRIES tries. The secret number was $RANDOM_NUMBER. Nice job!"
    THE_NUMBER="FOUND"
    ADD_GAMES=$($PSQL "UPDATE users SET games_played = games_played + 1 WHERE username = '$USER_NAME'")
    ADD_BEST_GAMES=$($PSQL "UPDATE users SET best_game = $TRIES WHERE username = '$USER_NAME'")
    
  fi
}

GUESSING_GAME() {
TRIES=0
THE_NUMBER="NOT FOUND"
RANDOM_NUMBER=$(($RANDOM % 1000 + 1))

echo "Enter your username"
read USER_NAME
USER=$($PSQL "SELECT username, games_played, best_game FROM users WHERE username='$USER_NAME'")
if [[ -z $USER ]]
then
  echo "Welcome, $USER_NAME! It looks like this is your first time here."
  ADD_USER=$($PSQL "INSERT INTO users(username, games_played, best_game) VALUES('$USER_NAME', '0', '0')")
  echo "Guess the secret number between 1 and 1000:"
  GUESS_LOOP
    
else
  IFS="|" read USERNAME GAMES_PLAYED BEST_GAME <<< $USER
  echo -e "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
  echo "Guess the secret number between 1 and 1000:"
  GUESS_LOOP 
fi  
}

GUESSING_GAME

And the DATABASE is simple:
CREATE TABLE public.users (
user_id integer NOT NULL,
username character varying(25) NOT NULL,
games_played integer,
best_game integer
);GIT
As I understand the problem is output… Could you help me out with this?

You should count the last guess (the correct one) when counting the guesses. At the moment you count a guess only when it is wrong. (Your $TRIES is one too small)

Thank you, I’ve corrected my code.
But I still can’t pass:

  1. When you run your script, you should prompt the user for a username with Enter your username: , and take a username as input. Your database should allow usernames of at least 22 characters
    Does it mean that the program should accept usernames not shorter than 22 characters? I’m a bit confused.
    I’ve found your suggestion about this issue, that it should be VARCHAR(25). Unfortunately, it does not work for me

Try adding the semicolon to the end of this line as per the test case

Hi ksa2710199, i can say to you that VARCHAR(25) works for username. I made a basic users table and defined the other columns in the games table. And never defined a best_game column cause you can calculate every time. I’ll show you what´s work for me:

Mod edit: image redacted

Hi there, thanks for trying to help.

Pls don’t share your code with anyone though as sharing solutions is not allowed here.

Ok, it won’t happen again.

1 Like

Thank you a lot! My bad, did missed the semicolon

1 Like