Error: LoadError: UndefVarError: user_input not defined

I’m new in Julia, honestly starting few moments ago seeing a tweet! I’m facing an unexpected error. It looks everything fine my code but still it results an error. Can anyone help me out?

Code:

while user_input != "END"

    print("Enter some input, or END to quit: ")

    user_input = readline() #prompt the user input

end

error message:

ERROR: LoadError: UndefVarError: user_input not defined

Stacktrace:

 [1] top-level scope

   @ f:\Julia Master Course\While loops.jl:1

in expression starting at *:\++++++++(Privacy reason)\While loops.jl:1

Code editor: VS code

You haven’t defined user_input before using it. You probably need another user_input = readline() before the while loop

1 Like

Thanks for the feedback! I was following this tutorial from freecodecamp: Learn Julia For Beginners – The Future Programming Language of Data Science and Machine Learning Explained

Can you explain why my code results an error?

Still holds. The code at your link does not work without predefining user_input.

julia> while user_input != "End"
                  print("Enter some input, or End to quit: ")
                  user_input = readline() # Prompt the user for input
              end
ERROR: UndefVarError: user_input not defined

vs

julia> user_input=""; while user_input != "End"
                         print("Enter some input, or End to quit: ")
                         user_input = readline() # Prompt the user for input
                     end
Enter some input, or End to quit:
1 Like

Thanks for the answer! Kind of really excited with Julia so did not catch the mistake. But, have to say, Julia impressed me!