Build a number guessing game - Relational Database

Hi, i’m having trouble with only 3 user stories, and i can’t seem to understand what’s the probleam, when i test my script it works correctly in the terminal.

I’m having problems with :

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 that are 22 characters

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

If the username has not been used before, you should print Welcome, <username>! It looks like this is your first time here.

#!/bin/bash

PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"

GAME() {
  echo "Enter your username:"
  read USERNAME

  USERNAME_ID=$($PSQL "SELECT user_id FROM users WHERE name = '$USERNAME'")

  if [[ -z $USERNAME_ID ]] 
  then
    echo "Welcome, $USERNAME! It looks like this is your first time here."
    USERNAME_INSERT=$($PSQL "INSERT INTO users(name) VALUES('$USERNAME')")
  else
    GAMES=$($PSQL "SELECT COUNT(user_id) FROM games WHERE user_id = '$USERNAME_ID'")
    BEST=$($PSQL "SELECT MIN(tries) FROM games WHERE user_id = '$USERNAME_ID'")
    echo "Welcome back, $USERNAME! You have played $GAMES games, and your best game took $BEST guesses."
  fi
  NUMBER=$(( RANDOM % 1000 + 1 ))
  TRIES=0;
  GUESS=-1

  echo "Guess the secret number between 1 and 1000:"
  while [[ $GUESS != $NUMBER ]] 
  do
    TRIES=$(($TRIES+1))
    read GUESS
    if [[ ! $GUESS =~ ^[0-9]+$ ]]
    then
      echo "That is not an integer, guess again:"
    else
      if [[ $GUESS -gt $NUMBER ]]
      then
        echo "It's lower than that, guess again:"
      elif [[ $GUESS -lt $NUMBER ]]
      then
        echo "It's higher than that, guess again:"
      fi
    fi
  done
  echo "You guessed it in $TRIES tries. The secret number was $NUMBER. Nice job!"
  INSERT_GAME_DATA=$($PSQL "INSERT INTO games(tries, user_id) VALUES($TRIES,$USERNAME_ID)")
}

GAME

how is the table setup? Make sure you use varchar for the username with at least 22 (most people use 25).

For the number of tries, you should not increment the number of tries if I make an invalid guess (that is not a number). Only increment for valid guesses.
Also make sure you count the very last ‘correct’ guess.

The username can be as long as 30 chars, i will make some adjustments and see if it works

I’ve changed it so it only counts on valid guesses, still having the same probleam

Don’t forget to count the last valid guess (you weren’t doing that before)

Share the code again also.

#!/bin/bash

PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"

GAME() {
  echo "Enter your username:"
  read USERNAME

  USERNAME_ID=$($PSQL "SELECT user_id FROM users WHERE name = '$USERNAME'")

  if [[ -z $USERNAME_ID ]] 
  then
    echo "Welcome, $USERNAME! It looks like this is your first time here."
    USERNAME_INSERT=$($PSQL "INSERT INTO users(name) VALUES('$USERNAME')")
  else
    GAMES=$($PSQL "SELECT COUNT(user_id) FROM games WHERE user_id = '$USERNAME_ID'")
    BEST=$($PSQL "SELECT MIN(tries) FROM games WHERE user_id = '$USERNAME_ID'")
    echo "Welcome back, $USERNAME! You have played $GAMES games, and your best game took $BEST guesses."
  fi
  NUMBER=$(( RANDOM % 1000 + 1 ))
  TRIES=1;
  GUESS=-1

  echo "Guess the secret number between 1 and 1000:"
  while [[ $GUESS != $NUMBER ]] 
  do
    read GUESS
    if [[ ! $GUESS =~ ^[0-9]+$ ]]
    then
      echo "That is not an integer, guess again:"
    else
      if [[ $GUESS -gt $NUMBER ]]
      then
      TRIES=$(($TRIES+1))
        echo "It's lower than that, guess again:"
      elif [[ $GUESS -lt $NUMBER ]]
      then
      TRIES=$(($TRIES+1))
        echo "It's higher than that, guess again:"
      fi
    fi
  done
  echo "You guessed it in $TRIES tries. The secret number was $NUMBER. Nice job!"
  INSERT_GAME_DATA=$($PSQL "INSERT INTO games(tries, user_id) VALUES($TRIES,$USERNAME_ID)")
}

GAME

I’m counting the first try, if the user get it right in the first go it count one

I was able to make it pass:

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 that are 22 characters

and

If the username has not been used before, you should print Welcome, <username>! It looks like this is your first time here.

just by reordering the INSERT_GAME_DATA and the last echo

now only

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

won’t go, although it seems to be right

please post the newest code after your last rewrite

Hi, so I gave up in the code i was writing and rewrote the entire application and got it to work, almost the same logic, still don’t know what was causing the probleam

I am glad you got it to work. Since I didn’t see your code after your rewrite I can’t comment further.

I undertand, thanks for the help anyway, i used what you’ve said to rewrite the code

1 Like