Code Snippet: User Input-Multi Line

From Coder Merlin
Revision as of 18:49, 24 December 2018 by NerdOfLinux (talk | contribs)
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Reading multiple lines of text from the console[edit]

Note: Input read from the console is terminated with a ^D (Control-D) which indicates the end-of-file.

Swift[edit]

// Read multiple lines from the console until end-of-file
var line : String?
repeat {
    line = readLine()                // Read a single line of input from the console
    if let text = line {             // Let's check if it's not nil, if so, it's really a string
        print("You typed: \(text)")  // Print the string
    }
} while line != nil                  // Continue until end-of-file

Python[edit]

text = ""
while text is not None:
    try:
        text = input()
    except:
        text = None
        continue
    print("You typed: %s" %text)