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.
#!/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
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
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