Code Snippet: Prompt for Yes or No

From Coder Merlin
Jump to navigation Jump to search

Prompt the user to input either a "yes" or a "no"[edit]


// Prompt the user for 'yes' or 'no'
var line : String?
repeat {
     print("Please enter 'yes' or 'no' ->", terminator:"")
     line = readLine()
} while (line == nil || (line! != "yes" && line! != "no")) 

// By the time we get here, line! is either "yes" or "no"


text = input("Please enter 'yes' or 'no' ->")
while text != "yes" and text != "no":
    text = input("Please enter 'yes' or 'no' ->")


Scanner input = new Scanner(; // don't forget to import Scanner in java.util
System.out.print("Please enter 'yes' or 'no': "); // use println to go to the next line
String userInput =; // for string in same line
// Look up Scanner java for more information

CoderMerlin™ proudly recommends:

Your purchase via these links helps keep  Coder Merlin™ 's orbuculum ablaze. As an Amazon Associate we earn from qualifying purchases. Thank you for your support!

Designed with pride in Silicon Valley, CA, USA