Red Baron
Red Baron

Reputation: 7672

How to print as a side-effect in Elixir?

I'm recreating the popular word game "Wordle" in Elixir. I want to give the user feedback if they don't type in a 5 letter word. so far I've got this:

  def play do
    guess = IO.gets("Guess a 5 letter word: ")
    guess = String.trim(guess)
    guess = String.downcase(guess)
    attempt(guess)
  end

  def attempt(@correct_word) do
    "You won!"
  end

  def attempt(guess) when byte_size(guess) != 5 do
    "Guess must be 5 letters"
    play()
  end

  def attempt(guess) when byte_size(guess) == 5 do
    determine_guess_result(guess)
    "You have x guesses left"
  end

the problem is that this line never shows: "Guess must be 5 letters" because I immediately call play() after so it's never returned from the function

how is this achieve in Elixir where I can show this line and then call play() again to give the user the next turn?

Upvotes: 0

Views: 131

Answers (1)

Adam Millerchip
Adam Millerchip

Reputation: 23129

Use IO.puts/2, which prints with a newline appended by default.

IO.puts("Guess must be 5 letters")
play()

Upvotes: 1

Related Questions