Input¶
So far every program has worked on values written into its source. To work on data from
outside, a program reads standard input with input and eof, and its command-line arguments
with args.
Reading lines¶
input() returns the next line of standard input without its newline. input("prompt ")
writes the prompt first, without a newline, for programs someone runs by hand:
name = input("what is your name? ")
print("hello, " + name)
Once the input runs out, input() returns "". An empty line also gives "", so to read
every line, loop until eof() says nothing is left:
// reads "name score" lines from stdin and reports the best score
best_name = ""
best = 0.0
count = 0
total = 0.0
while not eof();
line = input().strip()
if line == "";
cont
fields = line.split()
score = float(fields[1])
total = total + score
count = count + 1
if count == 1 or score > best;
best_name = fields[0]
best = score
print("read " + str(count) + " scores")
print("best:", best_name, best)
print("average:", total / float(count))
Given this input,
ada 91.5
grace 88
linus 72.25
margaret 97
it prints
read 4 scores
best: margaret 97.0
average: 87.1875
Run it with the input redirected from a file, or piped from another program:
stone run scores.st < scores.txt
cat scores.txt | stone run scores.st
Turning text into values¶
Everything read is a str, so a few builtins turn text into other values and back:
line.strip()removes spaces, tabs, and line breaks from both ends.line.split()breaks a line into words at runs of whitespace, andline.split(",")breaks it at each comma, keeping empty fields.int("42")andfloat("2.5")read numbers, allowing whitespace around them. Text that is not a number, such asint("4x"), stops the program with an error naming the text, likeinvalid literal for int() with base 10: '4x'.str(value)goes the other way, giving the textprintwould write, so"n = " + str(3)is"n = 3".
Command-line arguments¶
args() returns the arguments given after the program as a list[str], without the program’s
own name:
total = 0
for a in args();
total = total + int(a)
print(total)
stone run sum.st 1 2 3 # prints 6
stone build sum.st -o sum && ./sum 1 2 3 # prints 6 too
Everything after the file belongs to the program, even arguments starting with -, so
stone run sum.st -5 --x gives it ["-5", "--x"].
Next, Errors.