Mix.install([
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"},
{:tested_cell, github: "brooklinjazz/tested_cell"},
{:utils, path: "#{__DIR__}/../utils"}
])
Ensure you type the ea
keyboard shortcut to evaluate all Elixir cells before starting. Alternatively you can evaluate the Elixir cells as you read.
With Elixir installed we can execute an Elixir file ending in .ex
or .exs
.
Elixir treats both file extensions similarly. However, we generally use .exs
files for scripts.
Let's create an Elixir script. First, create a script.exs
file with the following content.
IO.puts("Hello, world!")
Then execute the file from the command line. Any Elixir code inside of the file will execute.
$ elixir script.ex
Hello, world!
We can leverage the built-in IO module to retrieve user input and print information to the console.
Let's build a simple number guessing game to see how we can apply this to our script.
The game will generate a number between 1
and 10
, and prompt the user to answer. If their answer is correct, it will print "Correct!"
.
$ elixir guessing_game.exs
Guess a number between 1 and 10.
2
Correct!
If their answer is incorrect, it will print "Incorrect!"
.
$ elixir guessing_game.exs
Guess a number between 1 and 10.
3
Incorrect!
First, create a guessing_game.exs
script.
We can use the IO.gets/2 function to accept some user input and bind it to a variable. Note IO.gets/2 does not work in Livebook.
The user input is a string, which we can parse with Integer.parse/2.
With the parsed integer, we can then compare it to a random integer generated with Enum.random/1.
We then print the result to the console with IO.puts/2.
guess = IO.gets("Guess a number between 1 and 10.")
{parsed_guess, _} = Integer.parse(guess)
answer = Enum.random(1..10)
if parsed_guess == answer do
IO.puts("Correct!")
else
IO.puts("Incorrect")
end
Currently, the guessing game ends after the player enters a single guess. How could you let the player guess until they get the correct answer?
There are multiple solutions. One solution is to use recursion. We can recursively call IO.gets/2 until the answer matches the player's guess.
To create a recursive function, we'll need a module. Copy the following code
into your guessing_game.exs
script.
defmodule GuessingGame do
def guess do
player_guess = IO.gets("Guess a number between 1 and 10.")
answer = Enum.random(1..10)
if player_guess == answer do
IO.puts("Correct!")
else
guess()
end
end
end
GuessingGame.guess()
Now we can keep guessing.
$ elixir guessing_game.exs
Guess a number between 1 and 10.
2
Incorrect!
Guess a number between 1 and 10.
3
Incorrect!
However, we have a bug. The answer
is re-bound every time we recursively call GuessingGame.guess/0
.
Modify the guessing_game.ex
script so that the random answer
does not change each time the player guesses
a number.
Hint
Consider using module attributes, which are created at compile time and will not change.Generally, a script is a single .exs
file used for some singular purpose. While it is possible to load other
files with the Code module to split up functionality and create larger projects, it's
generally not something you will do on most projects, and therefore beyond the scope of this course.
Instead, it's more common to use the IEx shell or Mix to create larger projects with multiple files, both of which you will learn about in the next sections.
Run the following in your command line from the beta_curriculum folder to track and save your progress in a Git commit.
$ git add .
$ git commit -m "finish scripts section"