Push button to select between 2 LEDs

Hi there, I'm struggling with a school project. The requirements are that the arduino needs to communicate through the serial monitor to select between 2 LEDs, which will then be activated by a push button. The LED needs to stay on after the button has been pressed. Any advice on how to do this is greatly appreciated. Here's what I have so far.

const int buttonPin = 2;
const int yledPin = 4;
const int rledPin = 3;
boolean buttonState = LOW;
String val;

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(rledPin, OUTPUT);  
  pinMode(yledPin, OUTPUT);    
  Serial.begin(9600);
  Serial.print("Which LED would you like to use, red or yellow?");
}

void loop()
{
  while (Serial.available() == 0) {}
  val = Serial.readString();
  if (val == "red")
  {
    while (Serial.available() == 0) {
      buttonState = digitalRead(buttonPin);
      if (buttonState == HIGH) {
        digitalWrite(rledPin, HIGH);
        delay(1);
        Serial.println("The LED is on");
      }
      else if (buttonState == LOW) {
        digitalWrite(rledPin, LOW);
        delay(1);
        Serial.println("The LED is off");
      }
    }
  }
  if (val == "yellow")
  {
    while (Serial.available() == 0) {
      buttonState = digitalRead(buttonPin);
      if (buttonState == HIGH) {
        digitalWrite(yledPin, HIGH);
        delay(1);
        Serial.println("The LED is on");
      }
      else if (buttonState == LOW) {
        digitalWrite(yledPin, LOW);
        delay(1);
        Serial.println("The LED is off");
      }
    }
  }
}

If possible you should always post code directly in the forum thread as text using code tags (</> button on the toolbar). This will make it easy for anyone to look at it, which will increase the likelihood of you getting help. If the sketch is longer than the forum will allow then it’s ok to add it as an attachment but in this case the sketch is short so there is no good reason for doing this.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you’re using the Arduino Web Editor then you will not have access to this useful tool. I recommend using the standard Arduino IDE instead.

Please remove unnecessary blank lines from your code before posting to the forum. One or two to separate code into logical sections is fine but large spaces for no reason or random blank lines just make for more scrolling when we’re trying to read your code.

Please explain exactly what troubles you are having with your current code.

  if(val = "red")

= != ==

    if(buttonState == HIGH) {
     digitalWrite(rledPin, HIGH);
     delay(1);
     Serial.println("The LED is on");}
     else if(buttonState == LOW){

Do you plan later on to add code to deal with the other possible pin states? What would they be?

If the pin isn’t HIGH, there isn’t a snowball’s chance in hell that it is not LOW.

 if(val = "red")
  {
  while(Serial.available() == 0) { //Serial monitor waits for input from PC
     buttonState = digitalRead(buttonPin); //

You’ve already been told which color LED to light up. Why are you waiting for more serial input that you don’t read?

Do you plan later on to add code to deal with the other possible pin states? What would they be?

I don't know, but that's basically a rhetorical question as I have no idea what I'm doing. We got thrown in to the deep end in this class, having no prior experience with HDL.

If the pin isn't HIGH, there isn't a snowball's chance in hell that it is not LOW.

So else?

You've already been told which color LED to light up. Why are you waiting for more serial input that you don't read?

I thought that would keep the serial monitor open in case I wanted to change the color to yellow

having no prior experience with HDL.

What is HDL?

So else

Yes else but no need for the if after the else.

I thought that would keep the serial monitor open in case I wanted to change the color to yellow

No you have no choice over keeping the port open, and certainly what you did does nothing but hang on until at least one extra byte is received but not read.