How do I fix this broken walking animation start in C#?

Hello all,
Here is an extremely basic C# script that I wrote to test out a walking animation. I will go back later in the project and hopefully add booleans to control it so I can add jumping animations and the like. I have the walking animation all done. It is attached to the pistol model, and to make sure I dragged the animation to the pistol as well. I then dragged this script to it. I click play and it just plays the animation over and over no matter what I do. I just want some light shed on what I am doing wrong. Thanks.

 using UnityEngine;
 using System.Collections;
        
        
        
 public class walkinanimationpistol : MonoBehaviour {
        
 void Update () {
    if (Input.GetKeyDown ("w")) {
        	animation.Play("NameOfWalkingAnim");
    } else {
            animation.Stop("NameOfWalkingAnim");
    }
  }
}

you are in Update so pushing “w” start animation but in a frame because your are stopping the animation with if else
so make it like this

public class walkinanimationpistol : MonoBehaviour {

	void Update () {
		if (Input.GetKeyDown ("w")) {
//		    animation.Play("NameOfWalkingAnim");
			print("play");
		} else if (Input.GetKeyDown ("s")){
//		    animation.Stop("NameOfWalkingAnim");
			print("stop");
		}
	}
}