Number Guessing Game Step 8


How can I go past step 8 and 13?

Can we see your code?

1 Like
#!/bin/bash

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

echo "Enter your username:"
read USERNAME

USER_EXIST=$($PSQL "SELECT games_played, best_game FROM users WHERE username = '$USERNAME';")
if [[ -z $USER_EXIST ]]
then
  echo "Welcome, $USERNAME! It looks like this is your first time here."
  $PSQL "INSERT INTO users (username) VALUES ('$USERNAME');"
  GAMES_PLAYED=0
  BEST_GAME="N/A"
else
  IFS='|' read GAMES_PLAYED BEST_GAME <<< "$USER_EXIST"
  echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi

SECRET_NUMBER=$(( RANDOM % 1000 + 1 ))
echo "Guess the secret number between 1 and 1000:"
GUESSES=0

while true; do
  read GUESS
  ((GUESSES++))
  if ! [[ $GUESS =~ ^[0-9]+$ ]]; then
    echo "That is not an integer, guess again:"
    continue
  fi
  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:"
  else
    echo "You guessed it in $GUESSES tries. The secret number was $SECRET_NUMBER. Nice job!"
    NEW_GAMES_PLAYED=$(( GAMES_PLAYED + 1 ))
    if [[ $BEST_GAME == "N/A" || $GUESSES -lt $BEST_GAME ]]; then
      $PSQL "UPDATE users SET games_played=$NEW_GAMES_PLAYED, best_game=$GUESSES WHERE username='$USERNAME';"
    else
      $PSQL "UPDATE users SET games_played=$NEW_GAMES_PLAYED WHERE username='$USERNAME';"
    fi
    break
  fi
done```
--
-- PostgreSQL database dump
--

-- Dumped from database version 12.17 (Ubuntu 12.17-1.pgdg22.04+1)
-- Dumped by pg_dump version 12.17 (Ubuntu 12.17-1.pgdg22.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: users; Type: TABLE; Schema: public; Owner: freecodecamp
--

CREATE TABLE public.users (
    username character varying(22) NOT NULL,
    games_played integer DEFAULT 0 NOT NULL,
    best_game integer
);


ALTER TABLE public.users OWNER TO freecodecamp;

--
-- Data for Name: users; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--

INSERT INTO public.users VALUES ('user_1737986330488', 2, 706);
INSERT INTO public.users VALUES ('user_1737986330489', 5, 53);
INSERT INTO public.users VALUES ('user_1737986585673', 3, 518);
INSERT INTO public.users VALUES ('user_1737986585674', 6, 31);
INSERT INTO public.users VALUES ('user_1737986619073', 2, 249);
INSERT INTO public.users VALUES ('user_1737986619074', 5, 154);
INSERT INTO public.users VALUES ('user_1737987163859', 2, 255);
INSERT INTO public.users VALUES ('user_1737987163860', 5, 292);
INSERT INTO public.users VALUES ('user_1737987542226', 2, 96);
INSERT INTO public.users VALUES ('user_1737987542227', 5, 403);
INSERT INTO public.users VALUES ('user_1737987793292', 2, 97);
INSERT INTO public.users VALUES ('user_1737987793293', 5, 216);
INSERT INTO public.users VALUES ('user_1737985846769', 2, 691);
INSERT INTO public.users VALUES ('user_1737985846770', 5, 251);
INSERT INTO public.users VALUES ('user_1737985855811', 2, 161);
INSERT INTO public.users VALUES ('user_1737985855812', 5, 398);
INSERT INTO public.users VALUES ('user_1737985862642', 2, 65);
INSERT INTO public.users VALUES ('user_1737985862643', 5, 370);


--
-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.users
    ADD CONSTRAINT users_pkey PRIMARY KEY (username);


--
-- PostgreSQL database dump complete
--


I gave your code a test @atdungo1. Don’t print any of the query responses to the console. The UPDATE 1 or whatever else is printed from queries. Put them in variables to not print those. e.g: TEMP=$($PSQL "command")