Learn/Nim/3. Reading input and arithmetic
Lesson 3 of 8
Sign in

Nim, lesson 3

Reading input and arithmetic

Your exercises receive their input on standard input (stdin), one line at a time, as if someone typed it. Nim's standard input is the stdin file.

Reading a line

readLine(stdin) reads one line and gives it back as a string, without the line break. You will often see it written in method style, stdin.readLine(); Nim lets you call any procedure as first.proc(rest), and both forms mean the same thing.

If there is no line left to read, readLine raises an EOFError and the program stops. The exercises always give you the lines they promise, so for now that is fine.

Text to numbers

Input is always text, and Nim will not mix text and numbers for you: "7" + 1 does not compile. The module has the converters you need:

2 exercises to try what you just read.

Exercise 1

Pizza party

Press Ctrl Enter or Check to test your code.

Exercise 2

Celsius to Fahrenheit

Press Ctrl Enter or Check to test your code.

strutils
  • strip removes spaces and line-ending characters from both ends, which protects you from stray whitespace in the input.
  • parseInt turns "42" into the int 42.
  • parseFloat turns "36.6" (or "37") into a float.

Both raise a ValueError if the text is not a valid number.

Arithmetic

ExpressionResultMeaning
7 + 29add
7 - 25subtract
7 * 214multiply
7 / 23.5divide, always a float
7 div 23whole-number division
7 mod 21remainder

div and mod are words, not symbols. Multiplication and division happen before addition and subtraction, and parentheses change the order.

Mixing ints and floats

A number literal written in your code adapts to its neighbour, so celsius * 9 works fine when celsius is a float. Two variables of different types are another story: Nim never silently turns an int variable into a float. You convert it yourself:

This strictness can feel fussy at first, but it means a number never changes type by accident.

Further reading

  • The strutils module (strip, parseInt, parseFloat)
  • The syncio module (readLine, stdin)
  • Nim Tutorial (Part I): integers, floats and type conversion

A group shares some pizza slices equally. Every person gets the same whole number of slices, and the rest are left over.

Input: two lines. The first is the number of slices (0 or more), the second the number of people (1 or more).

Output: two lines:

That sample is for the input:

Read a temperature in degrees Celsius and print it in Fahrenheit, using F = C * 9 / 5 + 32.

Input: one line with a number, which may have a decimal part (36.6) or not (100).

Output: just the Fahrenheit value. Any normal float form is accepted, so 212.0 and 212 both count for 100 °C.

prints

let name = stdin.readLine()
echo "Hi, ", name, "!"
import std/strutils

let age = stdin.readLine().strip().parseInt()
let height = parseFloat("1.75")
let people = 4
let bill = 50.0
echo bill / toFloat(people)    # 12.5
echo bill / float(people)      # the same: a type name converts too
echo int(12.9)                 # 12, the fractional part is dropped
Each person gets 3 slices.
Leftover: 2
14
4
37
98.6