Animation doesn't go back to idle in unity

i have a script made in c# to move my player here is the script:

using UnityEngine;
using System.Collections;

public class player_move : MonoBehaviour {

	public float speed = 3.0F;
	public float jump = 8.0f;
	public float gravity = 1.0f;
	private Vector3 movePlayer = Vector3.zero;

	void Update() {
		CharacterController controller = GetComponent<CharacterController>();
		if (controller.isGrounded) {

			movePlayer = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

			movePlayer = transform.TransformDirection(movePlayer);


			if (Input.GetKey ("up")){
				controller.Move (movePlayer * speed * Time.deltaTime);
				animation.Play ("moving");
			}
			if (Input.GetKey ("left")){
				controller.Move (movePlayer * speed * Time.deltaTime);
			}      
			if (Input.GetKey ("down")){      
				controller.Move (movePlayer * speed * Time.deltaTime); 
				animation.Play ("moving");
			}
			
			if (Input.GetKey ("right")){     
				controller.Move (movePlayer * speed * Time.deltaTime); 
			}

			if (Input.GetKeyDown(KeyCode.Space)){
				movePlayer = transform.TransformDirection(Vector3.up);
				movePlayer.y -= gravity * Time.deltaTime;
				controller.Move (movePlayer);
			}

			else if(Input.GetKeyUp ("up") || Input.GetKeyUp ("down")){

				animation.Play ("idle");
			}
		}

	}

}

what i am trying to do is when the user presses up or down arrow key then it should move and play an animation. But as soon the user releases the key, it should go back to the idle animation and not freeze the arms in mid-air.

Try this:

else if(!Input.GetKey("up") || !Input.GetKey("down")){
    animation.Play("idle"); //make sure this name is correct
}

Or you could change up your input script by doing this:

using UnityEngine;
using System.Collections;

public class player_move : MonoBehaviour {
    public float speed = 3.0F;
    public float jump = 8.0f;
    public float gravity = 1.0f;
    private Vector3 movePlayer = Vector3.zero;

    void Update() {
        CharacterController controller = GetComponent<CharacterController>();
        if(controller.isGrounded) {
            movePlayer = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
            movePlayer = transform.TransformDirection(movePlayer);

            if(Input.GetKey("up") || Input.GetKey("down")) {
                controller.Move(movePlayer * speed * Time.deltaTime);
                animation.Play("moving");
            }
            else
                animation.Play("idle");

            if(Input.GetKeyDown(KeyCode.Space)) {
                movePlayer = transform.TransformDirection(Vector3.up); //Not sure you need this line.
                movePlayer.y -= gravity * Time.deltaTime;
                controller.Move(movePlayer);
            }
        }
    }
}