FAQ: Conditional Statements - Review

This community-built FAQ covers the “Review” exercise from the lesson “Conditional Statements”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C#

FAQs on the exercise Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello, fellow learners!
I can’t get it why the system keeps telling me that I cannot convert a string to the double and cannot consequently cannot compare string and double. Please help!

double guess = 5;
      Console.WriteLine("Type any number from 1 to 10");
      string userGuess = Console.ReadLine();
     userGuess = Convert.ToDouble(userGuess);
      if (guess == userGuess)
      {
        Console.WriteLine("Correct");
      }
      else if (guess >= userGuess)
      {
        Console.WriteLine("Try higher");
      }
      else 
      {
        Console.WriteLine("Way too high");
      }

userGuess is of type string, so you cannot assign a double to userGuess. For string and double comparison make them both a double first (which is what you are trying to do) and then compare.

1 Like

Here is my code for the first suggestion of this challenge. Would there be a better way to write this?

using System;

namespace Review
{
  class Program
  {
    static void Main(string[] args)
    {
      /* use this space to write your own short program! 
      Here's what you learned:

      CONDITIONALS: if, if...else, else if
      SWITCH STATEMENT: switch (condition)
      TERNARY OPERATOR: (condition) ? true : false

      Good luck! */
      int realNumber = 6;
      
      Console.WriteLine("Pick a number between 1 and 10");
      int userInput = int.Parse(Console.ReadLine());
      
      if (userInput == realNumber)
      {
        Console.WriteLine("Correct!");
        
      }
      else if (userInput < realNumber)
      {
        Console.WriteLine("Too low");
      
      }
      else if (userInput > realNumber)
      {
        Console.WriteLine("Too high");
        
      }
      
    }
  }
}

1 Like

Hi, your code works great. I tried the option with the ternary operator just for fun and this is what I came up with:

using System;

namespace Review
{
  class Program
  {
    static void Main(string[] args)
    {
      int number = 6;
      Console.WriteLine ("Guess the number");
      int userGuess = int.Parse(Console.ReadLine());
      
     string message = (userGuess == number) ? "Correct" : (userGuess<number) ? "too low" : "too high";
      
      Console.WriteLine (message);

    }
  }
}

3 Likes

using System;

namespace SwitchStatement

{

class Program

{

static void Main(string[] args)

{

  Console.WriteLine("Choose a number from 10: ");



int answer = Console.ReadLine()

switch (answer)

{

case "1": 

 Console.WriteLine ("Way to low");

break;

case "2": 

 Console.WriteLine ("to low");

break;

case "3": 

 Console.WriteLine ("to low");

break; 

case "4": 

 Console.WriteLine ("low but almost there"); 

break; 

case "5": 

 Console.WriteLine ("Correct, nice job");

break; 

case "6": 

Console.WriteLine ("high but almost there"); 

break; 

case "7": 

 Console.WriteLine ("to high"); 

break;

case "8":

 Console.WriteLine ("to high");

break;

case "9":

 Console.WriteLine ("to high");

break;

case "10":

 Console.WriteLine ("Way to high"):

break;



default

 Console.WriteLine ("Invalid number");

break;

}

}

}

}

WHY ISN’T THIS CODE WORKING CAN SOMEONE PLEASE HELP ME

1 Like

This is my birthday checker. I have no idea how to do an ASCII celebratory banner so I didn’t.

            DateTime today = DateTime.Today;

            Console.WriteLine("Input your birth year: ");
            int year = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Input your birth month: ");
            int month = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Input your birth day: ");
            int day = Convert.ToInt32(Console.ReadLine());

            DateTime userBday = new DateTime(year, month, day);


            if (userBday.Month == today.Month && userBday.Day == today.Day)
            {
                Console.WriteLine("Congratulations, it's your birthday!");
            }
            else {

                //We'll add your age to your birthday year, so that we establish your birthday date on the current year.
                DateTime next = userBday.AddYears(today.Year - userBday.Year);

                // checks if the current year's birthday is less (already passed) than the current date. If so, we add another year to create the next year's birthday date.
                if (next < today) 
                    next = next.AddYears(1);

                int numDays = (next - today).Days;
                Console.WriteLine("Your birthday is still "+ numDays + " days away.");
            }
3 Likes

This is my number guesser. I wanted to create one where the chosen number is always random and not fixed.

            Console.WriteLine("Guess a number between 1 to 10: ");

            Random rnd = new Random();
            int chosenNumber = rnd.Next(1, 11);

            int answer = Convert.ToInt32(Console.ReadLine());

            string result = (chosenNumber == answer) ? "correct" : (answer < chosenNumber) ? "too low" : 
            "too high";

            Console.WriteLine("Your guess was " + result + "! The number was " + chosenNumber);

Hallo everyone!
I have a problem for the review exercise, I can’t figure out why the system keeps telling me “syntax error” with my code. Can anybody tell me where I did wrong with my code?

 using System;
namespace Review
{
class Program
{
static void Main(string[] args)
{
 Console.WriteLine("please type a number between 1-10");
 int standard = 6;
 int a= int.Parse(Console.ReadLine());
 switch(a)
 {
   case a < 6:
      Console.WriteLine("too low");
      break;
  case a = 6:
      Console.WriteLine("correct");
      break;
  case a > 6:
      Console.WriteLine("too high");
      break;
  default:
      Console.WriteLine("please try again");
      break;
    }
  }
}
}

Not sure if you are still trying to figure it out but there were a couple of incorrect ; :
and some missing ;

also the line int answer = Console.ReadLine() should be string answer = Console.Readline();

I like that you used a switch statement and had a response for each number.

I made the changes to the code and this was working:

using System;

namespace Review

{

class Program

{

static void Main(string[] args)

{

  /* use this space to write your own short program! 

  Here's what you learned:

  CONDITIONALS: if, if...else, else if

  SWITCH STATEMENT: switch (condition)

  TERNARY OPERATOR: (condition) ? true : false

  Good luck! */

Console.WriteLine("Choose a number from 10: ");

string answer = (Console.ReadLine());

switch (answer)

{

case “1”:

Console.WriteLine (“Way to low”);

break;

case “2”:

Console.WriteLine (“to low”);

break;

case “3”:

Console.WriteLine (“to low”);

break;

case “4”:

Console.WriteLine (“low but almost there”);

break;

case “5”:

Console.WriteLine (“Correct, nice job”);

break;

case “6”:

Console.WriteLine (“high but almost there”);

break;

case “7”:

Console.WriteLine (“to high”);

break;

case “8”:

Console.WriteLine (“to high”);

break;

case “9”:

Console.WriteLine (“to high”);

break;

case “10”:

Console.WriteLine (“Way to high”);

break;

default:

Console.WriteLine (“Invalid number”);

break;

}

}

}

}

Hi

Complete code
Guess Game from 1-10 numbers using c#

Cheers

It keeps saying that I can’t convert string to an int. I used the Convert.ToInt32 like we did in an earlier lesson into the first suggestion. Any fixes?

using System;

namespace Review

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine(“geuss the number that is between 1 to 10.”);

//variables

int correctNumber;

correctNumber = “4”;

//Number geussing

int userNumber = Convert.ToInt32(Console.ReadLine());

if (userNumber == correctNumber)

{

Console.WriteLine(“Nice Job!”);

}

else

{

Console.WriteLine(“U suck. Get good”);

}

}

}

}

so idk but the ron guys code definitely doesnt work soooo… but uhm ive come to notice that strings cant be integers even when you are asking for a Console.ReadLine() im not sure why but the console just doesnt know the difference.

ive noticed though for the integers to be acceptable people are doing
int userGuess = int.Parse(Console.ReadLine());
they are making the console allow a integer to be used when entering in something .

This is how i did it. At first i try to make

Case (var <= x)

but that is not allowed so after some googling i got the answer.

Console.WriteLine(“Choose a number between 1-10”);

        int result = int.Parse(Console.ReadLine());           


        switch (result)
        {

            case int number when (result <= 5):
                Console.WriteLine("Wrong");
                break;

           case (6):
                Console.WriteLine("Correct!");
                break;

            case int number when  result >= 7 && result <= 10:
                Console.WriteLine("Too high");
                break; 
        }

For anyone interested in a completely over engineered version of the ‘Guess a number’ game, please see the following. I would appreciate any feedback!

> using System;
> 
> using System.Linq;
> 
> namespace Review
> 
> {
> 
>   class Program
> 
>   {
> 
>     static void Main(string[] args)
> 
>     {
> 
>       Random r = new Random();
> 
>       int rInt = r.Next(1, 10);
> 
>       int guess = 0;
> 
>       string userInput = "";
> 
>       bool letterCheck = true;
> 
>       bool symbolCheck = true;
> 
>       bool punctuationCheck = true;
> 
>       Console.WriteLine("Guess a number between 1 and 10.");
> 
>       do
> 
>       {
> 
>         userInput = Console.ReadLine();
> 
>         letterCheck = userInput.Any(char.IsLetter);
> 
>         symbolCheck = userInput.Any(char.IsSymbol);
> 
>         punctuationCheck = userInput.Any(char.IsPunctuation);
> 
>         if (letterCheck || symbolCheck || punctuationCheck)
> 
>         {
> 
>           Console.WriteLine("You need to guess a NUMBER and it needs to be between 1 and 10.");
> 
>         }
> 
>         else
> 
>         {
> 
>           guess = int.Parse(userInput);
> 
>           if (guess > rInt && guess <= 10)
> 
>           {
> 
>             Console.WriteLine("Too high! Guess again.");
> 
>           }
> 
>           else if (guess == rInt)
> 
>           {
> 
>             Console.WriteLine("Wow, you got it!");
> 
>           }
> 
>           else if (guess < rInt && guess >= 1)
> 
>           {
> 
>             Console.WriteLine("Ooph... too low. Guess again.");
> 
>           }
> 
>           else
> 
>           {
> 
>             Console.WriteLine("Your guess needs to be anything between 1 and 10. Guess again.");
> 
>           }
> 
>         }
> 
>       } while (guess != rInt);
> 
>     }
> 
>   }
> 
> }