Question

In: Computer Science

Matlab code Here is the command that asks the user if he/she wants to play again:...

Matlab code

Here is the command that asks the user if he/she wants to play again:

playAgain = input(Would you like to play again? (y/n): ', 's');

Your task is to write a validation loop that only strings that start with a letter "y" or a letter "n", irrespective of cases, are considered to be valid input.

Solutions

Expert Solution

Program:

clc;

playAgain = input('Would you like to play again? (y/n): ', 's'); % Accept input


while(ne(playAgain,'Y') && ne(playAgain,'y') && ne(playAgain,'N') && ne(playAgain,'n')) % validate the input is correct or not
  
disp('Not a valid choice'); % it print not a valid choice
  
playAgain = input('Would you like to play again? (y/n): ', 's'); % Accept input

end

disp('Valid Input!') % print valid input

Output:


Related Solutions

Write a program where a user of this program will play a game in which he/she...
Write a program where a user of this program will play a game in which he/she needs to guess a target number, which is a number that the program has randomly picked in the range that the user chooses. The program will repeatedly prompt for the guessed number and provide a clue whether the guessed number is bigger or smaller than the target number, until the guessed number equals the target number.
Code in C ++ that asks the user for an integer and that prints whether or...
Code in C ++ that asks the user for an integer and that prints whether or not the number is divisible by three in the integers. If the number is not divisible by three, the program must write a message indicating what the remainder is obtained by dividing the number by three.
MATLAB Write a script which asks the user of the program to provide an initial horizontal...
MATLAB Write a script which asks the user of the program to provide an initial horizontal position, initial vertical position, initial velocity, and angle. Create a time vector spanning from zero seconds to 100 seconds incremented at 0.01 seconds. Call the function that you created in the previous problem to calculate the trajectory and velocities of the projectile. Find the maximum height of the projectile and the time at which it reaches that point. Write a neat sentence stating what...
Python. Write a code that asks the user to enter a string. Count the number of...
Python. Write a code that asks the user to enter a string. Count the number of different vowels ( a, e, i, o, u) that are in the string and print out the total. You may need to write 5 different if statements, one for each vowel. Enter a string: mouse mouse has 3 different vowels
CODE IN PYTHON 1. Write a program that asks the user for the number of slices...
CODE IN PYTHON 1. Write a program that asks the user for the number of slices of pizza they want to order and displays the total number of dollar bills they owe given pizza costs 3 dollars a slice.  Note: You may print the value an integer value. 2. Assume that y, a and b have already been defined, display the value of x: x =   ya+b    3. The variable start_tees refers to the number of UD T-shirts at the start...
Using the code below as a template, write a program that asks the user to enter...
Using the code below as a template, write a program that asks the user to enter two integers, and finds and prints their greatest common denominator (GCD) in Java! package cp213; import java.util.Scanner; public class Lab01 { /** * @param a * @param b * @return */ public static int gcd(int a, int b) { // your code here } /** * @param args */ public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int a = 0;...
Question 1. The following code asks the user to enter the password and check it with...
Question 1. The following code asks the user to enter the password and check it with the pre-defined password that stored in the system. If the entered password is correct, it will authenticate the user to log-in into the account, otherwise reject! Analyze the following code, how it works? Do you think this code is safe? Justify? Try to overwrite this code and report the outputs? Define any vulnerabilities in this code if exist? What we can do to make...
Let’s say that a seller has a house she wants to sell for $150,000. She asks...
Let’s say that a seller has a house she wants to sell for $150,000. She asks the real estate agent to list it for sale at $160,000, but tells the agent not to entertain any bid below $140,000. An interested buyer, who can afford a mortgage at current rates if the total price is below $145,000 (his limit), looks at the house and likes it enough to put in an offer of $138,000. This situation could be characterized as: Group...
in C please! Write a code that asks user for input N and calculates the sum...
in C please! Write a code that asks user for input N and calculates the sum of the first N odd integers. There should be a "pure" method that calculates the sum separately that Main method should call when printing the output.
Write Python code that asks the user for a password from the console and continues asking...
Write Python code that asks the user for a password from the console and continues asking for a password until the user enters a password that has greater than 10 characters. (language python)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT