Build a number guessing game - one problem

I cannot pass one test.


Zrzut ekranu 2022-11-03 103047

code snippet:
if [[ -z $GUESSER ]]
then
echo “Welcome, $USERNAME! It looks like this is your first time here.”
INSERT_USER_RESULT=$($PSQL “INSERT INTO games(games_played, username, best_game) VALUES(1, ‘$USERNAME’, 0)”)
else

GUESSER=$($PSQL "SELECT username FROM games WHERE username = '$USERNAME'")
GAMES_PLAYED=$($PSQL "SELECT games_played FROM games WHERE username = '$USERNAME'")
BEST_GAME=$($PSQL "SELECT best_game FROM games WHERE username = '$USERNAME'")

echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."

fi

Everything is displayed correctly. What could be wrong?

It’s possible test is expecting different numbers for some reason. Could you share complete code and db dump?

#!/bin/bash

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

NUMBER=$((RANDOM % 1000 + 1))
NUMBER_OF_GUESSES=0
GAMES_PLAYED=0

GUESS_LOOP() {
read GUESS

if [[ $GUESS =~ [1]+$ ]]
then
if [[ $GUESS = $NUMBER ]]
then
let NUMBER_OF_GUESSES++

echo "You guessed it in $NUMBER_OF_GUESSES tries. The secret number was $NUMBER. Nice job!"

#Update best game 
BEST_GAME=$($PSQL "SELECT best_game FROM games WHERE username = '$USERNAME'")
 if [[ $BEST_GAME -eq 0 || $NUMBER_OF_GUESSES -lt $BEST_GAME ]]
then
  UPDATE_BEST_GAME_RESULT=$($PSQL "UPDATE games SET best_game = '$NUMBER_OF_GUESSES' WHERE username = '$USERNAME'")
fi

else if [[ $GUESS > $NUMBER ]]
then
let NUMBER_OF_GUESSES++
echo “It’s lower than that, guess again:”
GUESS_LOOP
else
let NUMBER_OF_GUESSES++
echo “It’s higher than that, guess again:”
GUESS_LOOP
fi
fi
else
echo “That is not an integer, guess again:”
GUESS_LOOP
fi
}

echo “Enter your username:”
read USERNAME

GUESSER=$($PSQL “SELECT username FROM games WHERE username = ‘$USERNAME’”)

if [[ -z $GUESSER ]]
then
echo “Welcome, $USERNAME! It looks like this is your first time here.”
INSERT_USER_RESULT=$($PSQL “INSERT INTO games(games_played, username, best_game) VALUES(1, ‘$USERNAME’, 0)”)
else

GUESSER=$($PSQL "SELECT username FROM games WHERE username = '$USERNAME'")
GAMES_PLAYED=$($PSQL "SELECT games_played FROM games WHERE username = '$USERNAME'")
BEST_GAME=$($PSQL "SELECT best_game FROM games WHERE username = '$USERNAME'")

echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."

fi

#Update number of games played
GAMES_PLAYED=$($PSQL “SELECT games_played FROM games WHERE username = ‘$USERNAME’”)
let GAMES_PLAYED++
UPDATE_GAMES_PLAYED_RESULT=$($PSQL “UPDATE games SET games_played = ‘$GAMES_PLAYED’ WHERE username = ‘$USERNAME’”)

echo “Guess the secret number between 1 and 1000:”

GUESS_LOOP


  1. 0-9 ↩︎

– PostgreSQL database dump

– Dumped from database version 12.9 (Ubuntu 12.9-2.pgdg20.04+1)

– Dumped by pg_dump version 12.9 (Ubuntu 12.9-2.pgdg20.04+1)

SET statement_timeout = 0;

SET lock_timeout = 0;

SET idle_in_transaction_session_timeout = 0;

SET client_encoding = ‘UTF8’;

SET standard_conforming_strings = on;

SELECT pg_catalog.set_config(‘search_path’, ‘’, false);

SET check_function_bodies = false;

SET xmloption = content;

SET client_min_messages = warning;

SET row_security = off;

DROP DATABASE number_guess;

– Name: number_guess; Type: DATABASE; Schema: -; Owner: freecodecamp

CREATE DATABASE number_guess WITH TEMPLATE = template0 ENCODING = ‘UTF8’ LC_COLLATE = ‘C.UTF-8’ LC_CTYPE = ‘C.UTF-8’;

ALTER DATABASE number_guess OWNER TO freecodecamp;

\connect number_guess

SET statement_timeout = 0;

SET lock_timeout = 0;

SET idle_in_transaction_session_timeout = 0;

SET client_encoding = ‘UTF8’;

SET standard_conforming_strings = on;

SELECT pg_catalog.set_config(‘search_path’, ‘’, false);

SET check_function_bodies = false;

SET xmloption = content;

SET client_min_messages = warning;

SET row_security = off;

SET default_tablespace = ‘’;

SET default_table_access_method = heap;

– Name: games; Type: TABLE; Schema: public; Owner: freecodecamp

CREATE TABLE public.games (

games_played integer,

username character varying(16),

best_game integer

);

ALTER TABLE public.games OWNER TO freecodecamp;

– Data for Name: games; Type: TABLE DATA; Schema: public; Owner: freecodecamp

INSERT INTO public.games VALUES (2, ‘Aurora’, 0);

INSERT INTO public.games VALUES (13, ‘eva’, 8);

– PostgreSQL database dump complete

01
I deleted the old entries from the table and when I introduce new people, they show up correctly. (Aurora has 0 because I quit the game.)

00

Usernames are expected to allow at least 22 characters. I don’t know if that’s the only issue here. Please wrap the pasted code in the triple backticks so it’s formatted correctly:

```
pasted code
```