Difference between revisions of "Code Snippet: Prompt for Yes or No"

From Coder Merlin
Line 14: Line 14:


== Python ==
== Python ==
<syntaxhighlight lang="python">
text = input("Please enter 'yes' or 'no' ->")
while text != "yes" and text != "no":
    text = input("Please enter 'yes' or 'no' ->")
</syntaxhighlight>

Revision as of 18:52, 24 December 2018

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Read the contents of a file into a string[edit]

Swift[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"

Python[edit]

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