Project: True or False? Point 22. Extension of project

Hi there,
My question is about Project “True or False?”

I ve finished all points except the last one 22. It is about to wrap the code into a method, so that you just run

RunQuiz(questions, answers)

below is the original code slightly modified, point 1 - 21:

what I ve done so far:

  1. I added “return” at the very end as I would like the method to return the score.
  2. I tried to wrap the whole code into a method by

static int RunQuiz (string questions, bool answers)
{
PASTED MY CODE HERE
}

I look up in the syllabus, but the examples how to create a method is so simple and now I am facing this code, that is overwhelming me…

My thoughts: in the method declaration I have the input parameters, the questions and the answers. Questions is an array of strings, answers an array of bools.
So before I call the method, I need to declare the array of questions and answers BEFORE static int RunQuiz (string [] questions, bool [] answers), then I call the method that runs through the code asking and using ReadLines and then returning the score…
However, when I run it like this it says…

error: cs 8370…Can someone please help?

static int RunQuiz (string [] questions, bool [] answers)



//1.

      string [] questions = new string [] 

      {

        "Is \"Tree\" a plant?", "Is my name Peter?", "Am I an old guy?"

      };

      //2.

      bool [] answers = new bool [] 

      {

        true,false,false

      };

      //3.

      bool [] responses = new bool [questions.Length];

      //4.

      if (questions.Length != answers.Length)

      {

        Console.WriteLine($"The amount of questions and answers do not match!\n{questions.Length} questions\n{answers.Length} answers");

      }

      //5.

      int askingIndex = 0;

      

      //6.

      foreach (string question in questions)

      {

      

        //7.

        string input;

        bool isBool;

        bool inputBool;

        //8.

        Console.WriteLine($"{questions[askingIndex]}\nTrue or False?");

        input=Console.ReadLine();

        //9.

        isBool = Boolean.TryParse(input,out inputBool);

        //10.

        //Test code dotnet run

        //11. 12.

        while (isBool==false)

        {

          Console.WriteLine("Please respond with \"True\" or \"False\"");

          input=Console.ReadLine();

          isBool = Boolean.TryParse(input,out inputBool);        

        }

        //13.

        responses[askingIndex] = inputBool;

        askingIndex++;

      }

        //14.

        //int respondIndexTest = 0;

        //foreach (bool response in responses)

        //{

        //  Console.WriteLine($"Respond {respondIndexTest+1}: {responses[respondIndexTest]}\n");

        //  respondIndexTest++;

        //}

        //15.

        int scoringIndex = 0;

        int score = 0;

        //16.

        foreach (bool answer in answers)

        {        

        //17.

          bool resp = responses[scoringIndex];

        //18.

          bool ans = answers[scoringIndex];

        //19.

          if(resp==ans)

          {

            score++;

          }

        //20.

          scoringIndex++;

        } 

        

        //21.

        return Console.WriteLine($"You got {score} out of {questions.Length} correct!");

Hello, @adis_1102.

I formatted your code above. To format large blocks of code, rather than using Blockquote, use the Preformatted text button </>. Your code needs to appear between otherwise blank lines that each contain three back ticks:

```
code goes here
```

Could you post all of your code in a reply?

I changed the code as the RunQuiz() was inside the Main(), however, it still doesnt work…

using System;

namespace TrueOrFalse
{
  class Program
  {
		static void Main(string[] args)
    {
      // Do not edit these lines
      Console.WriteLine("Welcome to 'True or False?'\nPress Enter to begin:");
      string entry = Console.ReadLine();
      Tools.SetUpInputStream(entry);


      
      // Type your code below
      string [] MyQuestions = new string [] 
      {
        "Is \"Tree\" a plant?", "Is my name Peter?", "Am I an old guy?"
      };

      bool [] MyAnswers = new bool [] 
      {
        true,false,false
      };

      RunQuiz(MyQuestions, MyAnswers);
    }
    

  static void RunQuiz (string [] questions, bool [] answers) 
    {
      //1.
      string [] questions = new string [] 
      {
        "Is \"Tree\" a plant?", "Is my name Peter?", "Am I an old guy?"
      };

      //2.
      bool [] answers = new bool [] 
      {
        true,false,false
      };

      //3.
      bool [] responses = new bool [questions.Length];

      //4.
      if (questions.Length != answers.Length)
      {
        Console.WriteLine($"The amount of questions and answers do not match!\n{questions.Length} questions\n{answers.Length} answers");
      }

      //5.
      int askingIndex = 0;
      

      //6.
      foreach (string question in questions)
      {
      
        //7.
        string input;
        bool isBool;
        bool inputBool;

        //8.
        Console.WriteLine($"{questions[askingIndex]}\nTrue or False?");
        input=Console.ReadLine();

        //9.
        isBool = Boolean.TryParse(input,out inputBool);

        //10.
        //Test code dotnet run

        //11. 12.
        while (isBool==false)
        {
          Console.WriteLine("Please respond with \"True\" or \"False\"");
          input=Console.ReadLine();
          isBool = Boolean.TryParse(input,out inputBool);        
        }

        //13.
        responses[askingIndex] = inputBool;
        askingIndex++;
      }
        //14.
        //int respondIndexTest = 0;
        //foreach (bool response in responses)
        //{
        //  Console.WriteLine($"Respond {respondIndexTest+1}: {responses[respondIndexTest]}\n");
        //  respondIndexTest++;
        //}

        //15.
        int scoringIndex = 0;
        int score = 0;

        //16.
        foreach (bool answer in answers)
        {        
        //17.
          bool resp = responses[scoringIndex];
        //18.
          bool ans = answers[scoringIndex];
        //19.
          if(resp==ans)
          {
            score++;
          }
        //20.
          scoringIndex++;
        } 
        
        //21.
        Console.WriteLine($"You got {score} out of {questions.Length} correct!");
    }
  }
}

Thanks for formatting it. I posted my code in a reply

Do you get an error message? The Codecademy learning environment is very finicky when it comes to while loops. I got your code to work yesterday, but it wouldn’t save on CC unless I commented out the while loop in the RunQuiz method. If you paste your code in another environment like repl.it it may work. You can work around the while loop issue on CC by changing any input other than true to false rather than asking for correct input until they answer true or false.

Thanks for looking into it. It does work now.
And the issue was that I had declared the input parameters twice
string MyQuestions and bool MyAnswers in Main()
string questions and bool answers in static RunQuiz().
From my not working code above:

.
.
.
static void Main(string[] args)
    {
      // Do not edit these lines
      Console.WriteLine("Welcome to 'True or False?'\nPress Enter to begin:");
      string entry = Console.ReadLine();
      Tools.SetUpInputStream(entry);


      
      // Type your code below
      string [] MyQuestions = new string [] 
      {
        "Is \"Tree\" a plant?", "Is my name Peter?", "Am I an old guy?"
      };

      bool [] MyAnswers = new bool [] 
      {
        true,false,false
      };

      RunQuiz(MyQuestions, MyAnswers);
    }
    

  static void RunQuiz (string [] questions, bool [] answers) 
    {
      //1.
      string [] questions = new string [] 
      {
        "Is \"Tree\" a plant?", "Is my name Peter?", "Am I an old guy?"
      };

      //2.
      bool [] answers = new bool [] 
      {
        true,false,false
      };
.
.
.

my new working code (incl. the while loop)

using System;

namespace TrueOrFalse
{
  class Program
  {
		static void Main(string[] args)
    {
      // Do not edit these lines
      Console.WriteLine("Welcome to 'True or False?'\nPress Enter to begin:");
      string entry = Console.ReadLine();
      Tools.SetUpInputStream(entry);


      
      // Type your code below
      string [] MyQuestions = new string [] 
      {
        "Is \"Tree\" a plant?", "Is my name Peter?", "Am I an old guy?"
      };
     bool [] MyAnswers = new bool [] 
      {
        true,false,false
      };

      RunQuiz(MyQuestions, MyAnswers);
    }
    

  static void RunQuiz (string [] questions, bool [] answers) 
    {
      //1.
    //  string [] questions = new string [] 
    //  {
    //    "Is \"Tree\" a plant?", "Is my name Peter?", "Am I an old guy?"
    //  };
    //
    //  //2.
    //  bool [] answers = new bool [] 
    //  {
    //    true,false,false
    //  };

      //3.
      bool [] responses = new bool [questions.Length];

      //4.
      if (questions.Length != answers.Length)
      {
        Console.WriteLine($"The amount of questions and answers do not match!\n{questions.Length} questions\n{answers.Length} answers");
      }

      //5.
      int askingIndex = 0;
      

      //6.
      foreach (string question in questions)
      {
      
        //7.
        string input;
        bool isBool;
        bool inputBool;

        //8.
        Console.WriteLine($"{questions[askingIndex]}\nTrue or False?");
        input=Console.ReadLine();

        //9.
        isBool = Boolean.TryParse(input,out inputBool);

        //10.
        //Test code dotnet run

        //11. 12.
        while (isBool==false)
        {
          Console.WriteLine("Please respond with \"True\" or \"False\"");
          input=Console.ReadLine();
          isBool = Boolean.TryParse(input,out inputBool);        
        }

        //13.
        responses[askingIndex] = inputBool;
        askingIndex++;
      }
        //14.
        //int respondIndexTest = 0;
        //foreach (bool response in responses)
        //{
        //  Console.WriteLine($"Respond {respondIndexTest+1}: {responses[respondIndexTest]}\n");
        //  respondIndexTest++;
        //}

        //15.
        int scoringIndex = 0;
        int score = 0;

        //16.
        foreach (bool answer in answers)
        {        
        //17.
          bool resp = responses[scoringIndex];
        //18.
          bool ans = answers[scoringIndex];
        //19.
          if(resp==ans)
          {
            score++;
          }
        //20.
          scoringIndex++;
        } 
        
        //21.
        Console.WriteLine($"You got {score} out of {questions.Length} correct!");
    }
  }
}

what is still confusing to me is that in the method definition I do use the the string array questions and also the bool array answers in the code (see below the if statement), however, now it is substituted with the input parameters. So if I would use several string arrays, I would define it in Main(), but work work with other variables (also string variables) within the method - How does the system knows which input parameter belongs to which string variable within the method? Just by sorting?

 if (questions.Length != answers.Length)
      {
        Console.WriteLine($"The amount of questions and answers do not match!\n{questions.Length} questions\n{answers.Length} answers");
      }

Not sure I fully understand your question, but if you had multiple arrays of questions and answers defined in your Main method, you could pass whichever ones you wanted as arguments to your RunQuiz method. The parameters in your RunQuiz method would be assigned to the arrays that you passed, so the method should function the same for any of them.

Lets look at the code for an example, just to make sure we understand each other:

from the RunQuiz method:

bool [] responses = new bool [questions.Length];

and

foreach (string question in questions)...

“questions” is not defined anywhere, as well as answers.

However, I did define:

string [] MyQuestions =...

and

bool [] MyAnswers =..

and also passed these 2 variables as parameters to the RunQuiz method:

RunQuiz(MyQuestions, MyAnswers);

So when the following code block is run in RunQuiz:

`foreach (string question in questions)...`

How does the system knows that “questions” belongs to “MyQuestions” from Main() and not to “MyAnswers”

The only assumption I have right now is that the first array I defined ( string [] MyQuestions ) is the first parameter I pass static void RunQuiz (string [] questions, bool [] answers) , and the second array I define ( bool [] MyAnswers ) is the second parameter I pass.

I was expecting that I need to link them somehow like:

string [] MyQuestions = questions

and

bool [] MyAnswers = answers

sorry if I am complicated but I am struggling with that question and I really wanna know…

In this case, the arguments passed are assigned to the parameters by position. The first parameter in the parameter list is assigned to the value of the first argument passed to the method in the method call. The second parameter is assigned to the value of the second argument in the argument list, etc.

using System;

class MainClass {
  public static void Main (string[] args) {
    
    string animal = "cat";
    string name = "Morris";

    Console.WriteLine(SomeMethod(animal, name));
  }

  static string SomeMethod(string a, string n)
  {
    return $"I have a {a} named {n}.";
  }
}

Output:

I have a cat named Morris.

If we change the order of the argument list, we will change the output:

using System;

class MainClass {
  public static void Main (string[] args) {
    
    string animal = "cat";
    string name = "Morris";

    Console.WriteLine(SomeMethod(name, animal));
  }

  static string SomeMethod(string a, string n)
  {
    return $"I have a {a} named {n}.";
  }
}

Output:

I have a Morris named cat.

The parameters of the method are assigned to the values of the arguments provided by their position in each list. We could add more variables, and choose any of them to send to the method. The position of the argument in the argument list of the method call will determine the output:

using System;

class MainClass {
  public static void Main (string[] args) {
    
    string animal1 = "cat";
    string name1 = "Morris";

    string animal2 = "dog";
    string name2 = "Hercules";
    
    Console.WriteLine(SomeMethod(animal1, name1));
    Console.WriteLine(SomeMethod(animal2, name2));
    Console.WriteLine(SomeMethod(animal2, name1));
    Console.WriteLine(SomeMethod(name2, animal1));
    //we could even simply send string literals if we want
    //the method requires 2 string values
    //it doesn't matter if they are string literals or
    //variables assigned to string literals like above
    Console.WriteLine(SomeMethod("giraffe", "Jeff"));
    Console.WriteLine(SomeMethod("argument1", "argument2"));
  }

  static string SomeMethod(string a, string n)
  {
    return $"I have a {a} named {n}.";
  }
}

Output:

I have a cat named Morris.
I have a dog named Hercules.
I have a dog named Morris.
I have a Hercules named cat.
I have a giraffe named Jeff.
I have a argument1 named argument2.

Thanks a lot for the explanation!!

1 Like

You’re welcome. Hopefully that cleared things up a little.