Tell us what’s happening:
Cannot get test " 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"
to pass.
Every other test has passed. I’ve reviewed previous questions/answers regarding this issue and refactored my code into functions, but otherwise couldn’t find any other applicable solutions. My output in the terminal looks EXACTLY correct, but the test is not passing.
Any insight would be appreciated. Would be very helpful if we could just see the error logs for the failing tests…
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
function GUESSING {
local username=$1
local secret_number=$((RANDOM % 1000 + 1))
local attempts=0
echo "Guess the secret number between 1 and 1000:"
while true; do
read guess
# Check if the input is an integer
if [[ ! $guess =~ ^[0-9]+$ ]]; then
echo "That is not an integer, guess again:"
continue
fi
((attempts++))
if [ "$guess" -eq "$secret_number" ]; then
# Check if it's a new best game
local user_info=$($PSQL "SELECT games_played, best_game FROM users WHERE username = '$username'")
IFS="|" read games_played best_game <<< $user_info
if [ -z "$best_game" ] || [ "$attempts" -lt "$best_game" ]; then
# Update database with the new best game
echo "$attempts number of attempts"
local insert_best_game=$($PSQL "UPDATE users SET best_game = $attempts WHERE username = '$username'")
fi
# Update database with the number of games played
local update_user=$($PSQL "UPDATE users SET games_played = games_played + 1 WHERE username = '$username'")
echo "You guessed it in $attempts tries. The secret number was $secret_number. Nice job!"
break
elif [ "$guess" -lt "$secret_number" ]; then
echo "It's higher than that, guess again:"
else
echo "It's lower than that, guess again:"
fi
done
}
DISPLAY() {
echo "Enter your username:"
read username
# Check if the username exists in the database
user_info=$($PSQL "SELECT username, games_played, best_game FROM users WHERE username = '$username'")
if [ -n "$user_info" ]; then
# User exists
IFS="|" read user games_played best_game <<< $user_info
echo "Welcome back, $user! You have played $games_played games, and your best game took $best_game guesses."
else
# New user
insert_user=$($PSQL "INSERT INTO users (username) VALUES ('$username')")
echo "Welcome, $username! It looks like this is your first time here."
fi
# Call the GUESSING function
GUESSING $username
}
DISPLAY
--
-- 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: users; Type: TABLE; Schema: public; Owner: freecodecamp
--
CREATE TABLE public.users (
username character varying(30) NOT NULL,
games_played integer DEFAULT 0,
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 ('rob', 1, 10);
INSERT INTO public.users VALUES ('user_1705443263920', 2, 153);
INSERT INTO public.users VALUES ('user_1705443263921', 5, 108);
INSERT INTO public.users VALUES ('user_1705443293089', 2, 599);
INSERT INTO public.users VALUES ('user_1705443293090', 5, 333);
INSERT INTO public.users VALUES ('user_1705443392366', 2, 422);
INSERT INTO public.users VALUES ('user_1705443392367', 5, 42);
INSERT INTO public.users VALUES ('user_1705443442253', 2, 157);
INSERT INTO public.users VALUES ('user_1705443442254', 5, 2);
INSERT INTO public.users VALUES ('user_1705443456145', 2, 618);
INSERT INTO public.users VALUES ('user_1705443456146', 5, 626);
--
-- 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
--
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Challenge Information:
Number Guessing Game - Build a Number Guessing Game