# Learn Julia by Coding 7 Projects – Hands-On Programming Tutorial

The Julia programming language is used for a lot of really impactful and interesting challenges like Machine Learning and Data Science.

But before you can get to the complex stuff, it is worth exploring the basics to develop a solid foundation.

In this tutorial, we will go over some of the basics of Julia by building 7 small Julia projects:

• Guess the Number Game 💯
• Computer Number Guesser 🤖
• Rock 🗿, Paper 📃, Scissors ✂️
• Dice Rolling Simulator 🎲
• Countdown Timer ⏱️

It’s also worth noting that if you are totally new to Julia and want a comprehensive introduction to the language, you can check out this freeCodeCamp article.

## Beginner-Friendly July Projects

### How to Build Mad Libs in Julia ✍️

In Mad Libs, the user is prompted to enter different types of words. The random words the user enters are then inserted into a sentence. This leads to some pretty wacky and funny outcomes. Let’s try to program a simple version of this using Julia.

At the core of this problem, we want to concatenate (or add together) multiple strings so that we go from a sentence with some placeholders, to a sentence with the user input.

The simplest way to achieve this in Julia is with String Interpolation:

``julia> name = "Logan" "Logan" julia> new_string = "Hello, my name is \$name" "Hello, my name is Logan"``

Here we can see that we can insert the name variable we defined into the string by using the `\$name` syntax.

There are a bunch of other ways to do this, like using the `string` function:

``````julia> new_string = string("Hello, my name is ", name) "Hello, my name is Logan"
``````

but string interpolation seems the most straightforward and readable in this case.

Now that we know how we are going to set up the strings, we need to prompt the user for their input.

To do this, we can use the `readline` function as follows:

``````julia> my_name = readline()
Logan "Logan"``````

The `readline` function takes a single line of input from the user. This is exactly what we will want to use. Let’s put it all together into a simple example:

In this example, we learned how to work with strings, define a function, use print statements, and more!

As noted before, there are lots of other ways to do the same things we did above. So if you want to find out more about working with strings, check out the Julia docs here.

### How to Build a Guess the Number Game in Julia 💯

In this game, we will have to generate a random number and then try to guess what it is.

To begin, we will need to generate a random number. As always, there are many ways to do something like this but the most straightforward approach is to do the following:

``````julia> rand(1:10)
4``````

The `rand` function takes as input the range of numbers you want to use as the bounds for the number you will generate. In this case, we set the range as `1-10`, inclusive of both numbers.

The other new topic we need to cover for this example to work is while loops. The basic structure of a while loop is:

``````while some_condition is true do something
end``````

This loop will continue to iterate until the condition for the while loop is no longer met. You will see how we use this soon to keep prompting the user to enter a number until they guess it right.

Lastly, to make it a little easier for us, we are going to add an if statement which tells us if we guess a number that is close to the target number. The structure of an if statement in Julia is:

``````if some_condition is true do something
end``````

The big difference is that the if statement is checked once and then it is done. The initial condition is not re-checked unless the if statement is in a loop.

Now that we have the basic ideas down, let’s see the actual code to build the number guesser. Make sure you try this on your own before checking the solution below. Happy coding! 🎉

### How to Build a Computer Number Guesser in Julia 🤖

Now that we have seen what it looks like for us to try and guess what the computer randomly generated, let’s see if the computer can do any better.

In this game, we will select a number and then see how long it takes the computer to guess that number. To do this, we will introduce some new concepts like the Random module and for loops.

We’ll begin by thinking about how we can have the computer guess random numbers without repeating any.

One simple solution is to use the `rand` function, but the issue is that there’s no built-in way to make sure the computer doesn’t guess the same number more than once – since, after all, it is random!

We can solve this issue by combining the `collect` function and the `shuffle` function. We begin by defining a random seed:

``julia> rng = MersenneTwister(1234);``

Random seeds make it so that random number generators make reproducible results. Next, we need to define all possible guesses:

``````julia> a = collect(1:50)
50-element Vector{Int64}:
1
2
3
⋮``````

We now need to use the `shuffle` function to make the guesses random:

``````julia> using Random
julia> shuffle(rng, a)
50-element Vector{Int64}:
41
23
13
49
⋮``````

Now that we have the random guesses set up, it’s time to loop through them one at a time and see if the number is equal to the target the user inputted.

Again, give this a try before you check out the solution below:

### How to Build Rock 🗿, Paper 📃, Scissors ✂️ in Julia

If you have never played rock, paper, scissors, you are missing out! The basic gist is you try to beat your opponent with either rock, paper, or scissors.

In this game, rock beats scissors, scissors beat paper, and paper beats rock. If two people do the same one, you go again.

In this example, we will be playing rock, paper, scissors against the computer. We will also use the `sleep` function to introduce a short delay as if someone was saying the words out loud (which you would do if you played in person).

The sleep function takes in a number that represents how long you want (in seconds) to sleep. We can use this with a function or a loop to slow things down as you will see in this game.

``````sleep(1) # Sleep for 1 second
``````

Let’s also explore a function I found while writing this tutorial, `Base.prompt `, which helps us do what we were previously using `readline` for.

In this case, however, `prompt` auto-appends a `:` to the end of the line and allows us to avoid having two separate lines for the print and user input:

``````human_move = Base.prompt("Please enter 🗿, 📃, or ✂️")
``````

We will also need to use an `elseif` to make this example game work. We can chain `if` , `elseif` , and `else` together for completeness. Try putting together the if conditionals, prompts, and sleeps to get the desired behavior, and then check out the code below:

### How to Build a Password Generator in Julia 🎫

WARNING: Do not use this code to generate real passwords!

In the age of endless data breaches and people using the same password for every website, having a secure password is important. In this example, we will generate an arbitrary number of passwords with a variable length.

Given that this could take a long time, we will also add an external package, ProgressBars.jl, to visually show the progress of our for loop. If you have never added an external package before, consider checking out this robust tutorial on why the package manager is the most underrated feature of the Julia programming language.

To add a Julia package, open the REPL and type `]` followed by `add ProgressBars`. After that, as we did with the Random module (note we did not need to add it since it is part of base Julia), we can say `using ProgressBars` to load it in.

The main new idea we will introduce here is vectors / arrays. In Julia, we can put any type into an array. To create an empty array, we do:

``password_holder = []``

and then to add something, we use the `push!` function as you will see in the below example.

As mentioned before, we will use the ProgressBars package to show progress on the screen. Note that Julia is so quick that it likely won’t show you the loading screen unless you manually slow things down with a sleep function call or a high number of passwords. Check out the README for an example of using this in practice.

As with the other example, try to put some code together before you dissect the example below:

### How to Build a Dice Rolling Simulator in Julia 🎲

Dice are a fun way to explore and play around with randomness along with unicode characters.

Julia has amazing support for unicode, and if you want to see all the characters it supports, head to the Julia docs.

Let’s begin by defining an array of dice faces. To access unicode characters, we can use the Julia REPL to do tab completion by typing the following:

``julia> dicei``

followed by the tab button. This will create `⚀` which is “Die Face-1”. If we do this for all 6 sides of a 6 sided die, we end up with:

``dice_faces = ["⚀", "⚁", "⚂", "⚃", "⚄", "⚅"]``

For this game, we want to continually ask the user if they want to roll the dice. If they do, we generate a random number between 1 and 6 and then display the dice face from the array we created above.

Just like we did in previous projects, we will want to use the `rand` function as follows:

``rand(1:num_sides_dice)``

Give this a try before you check out one possible solution that is highlighted below and keep in mind how we could extend this or use this code to program a much larger game like Monopoly.

### How to Build a Countdown Timer in Julia⏱️

Countdowns, for better or worse, are a huge part of life. From New Years Eve to a parent frustratingly trying to convince a child to obey some rule, we see and participate in countdown timers regularly.

Now, we are going to get a chance to program one (yay). At the core, we will again be using the `sleep` function which we had a chance to play around with in the rock paper scissors example.

As a quick reminder, `sleep` takes in as an argument the number of seconds we want the program to pause for.

For this example, we are going to try to do some while loop nesting by using functions. We want to have a loop that continues to prompt the user if they want to set a timer, and then if they do, we call a function called `run_timer`. The `run_timer` function should prompt the user to enter how long they want the timer to run for.

The caveat here is that we also want to print our how long is left for the timer on each iterations. So if the user enters 5, we can’t just do `sleep(5)` since the user won’t be able to see anything happen for those 5 seconds.

Below is the main function which is given to you to start. Feel free to modify this as you see fit. Use this starter code and then define the `run_timer` function per the specification above.

Remember, there’s a lot of possible ways to approach this and the solution we include at the bottom is just one possible approach.

``````# Code from: https://github.com/logankilpatrick/Julia-Projects-for-Beginners function run_timer() # TODO
end # Call the run_timer function in a loop until the user quits it
function countdown_timer() # While the user chooses to run the countdown timer while true print("Do you want set a countdown timer? (1=Yes/0=No): ") answer = parse(Int64, readline()) # Convert the string value input to a number if answer == 1 # Run the timer run_timer() elseif answer == 0 println("Exiting...") break # Stop the countdown timer else println("Invalid input, please try again") end end end
countdown_timer()``````

Give it a shot and remember you will need to make use of the `parse`, `readline`, `sleep`, and `println` functions to make this function work.

## Wrapping up 🎁

I hope you had as much fun working with and reading about these projects as I did creating them.

If you want to make your own version of this post and make some small Julia projects and share them with the world, please do so and open a PR here: https://github.com/logankilpatrick/10-Julia-Projects-for-Beginners.

I can easily change the repo name to accommodate an influx of small projects.

I will also note that an exercise like this is also a great way to potentially contribute to Julia. While I was working on this post, I was able to open 2 PR’s to base Julia which I think will help improve the developer experience:

If you enjoyed this tutorial, let’s connect on Twitter.