Unexpected ERROR: UndefVarError: a not defined

Hey, I’m new to Julia and I have to program the game “paper soccer” for school. It’s almost finished, but now I got this “UndefVarError: a not defined” error. Since I’ve always adapted my code step by step, I really don’t know where this is comming from. Especially because “a” is the first variable I’ve defined in my code. This is what the error looks like:

UndefVarError: a not defined

  1. (::Main.workspace289.var"#1#22")()@ Local: 37
  2. #6 @ svg.jl:48 [inlined]
  3. #svg#40 (::Base.Iterators.Pairs{Symbol,String,Tuple{Symbol,Symbol,Symbol},NamedTuple{(:xmlns, :width, :height),Tuple{String,String,String}}}, ::typeof(NativeSVG.svg), ::NativeSVG.var"#6#7"{Main.workspace289.var"#1#22"}, ::Base.GenericIOBuffer{Array{UInt8,1}})@ svg.jl:94
  4. #Drawing#5 @ svg.jl:43 [inlined]
  5. top-level scope @ Local: 9

My code is quite long, but it’s doing 16 times basically the same
This is my code:


c = tuple(a, b)

spel = []

md"""
keuze: $(@bind keuze html"<input type=string>")\
"""
function input()
	if length(keuze) == 5
		push!(spel, keuze)
	end
end

begin
	input()
	C = []
	E = []
	T = []
	l = 2
	m = 10
	n = 8
		Drawing(width = join(["$(l*(n+2))", "cm"]), height = join(["$(l*(m+4))", "cm"])) do
			#bovenste deel van het bord
			g(fill = "none", stroke = "black", stroke_width = "0.5") do
				line(x1 = join(["$(((n/2))*l)", "cm"]), y1 = join(["$l", "cm"]), x2 = join(["$(((n/2)+2)*l)", "cm"]), y2 = join(["$l", "cm"]))
				for i in 0:2
					line(x1 = join(["$(((n/2)+i)*l)", "cm"]), y1 = join(["$l", "cm"]), x2 = join(["$(((n/2)+i)*l)", "cm"]), y2 = join(["$(2*l)", "cm"]))
				end
			end
			#middendeel van het bord
			for j in 1:m+1
				g(fill = "none", stroke = "black", stroke_width = "0.5") do
					line(x1 = join(["$l", "cm"]), y1 = join(["$((j+1)*l)", "cm"]), x2 = join(["$(l*(n+1))", "cm"]), y2 = join(["$((j+1)*l)", "cm"]))
				end
			end
			for j in 1:n+1
				g(fill = "none", stroke = "black", stroke_width = "0.5") do
					line(x1 = join(["$(l*j)", "cm"]), y1 = join(["$(2*l)", "cm"]), x2 = join(["$(l*j)", "cm"]), y2 = join(["$(l*(m+2))", "cm"]))
				end
			end
			#onderste deel van het bord
			g(fill = "none", stroke = "black", stroke_width = "0.5") do
				line(x1 = join(["$((n/2)*l)", "cm"]), y1 = join(["$((m+3)*l)", "cm"]), x2 = join(["$(((n/2)+2)*l)", "cm"]), y2 = join(["$((m+3)*l)", "cm"]))
				for i in 0:2
					line(x1 = join(["$(((n/2)+i)*l)", "cm"]), y1 = join(["$((m+2)*l)", "cm"]), x2 = join(["$(((n/2)+i)*l)", "cm"]), y2 = join(["$((m+3)*l)", "cm"]))
				end
			end
		if length(spel) ≥ 1
			for i in 1:length(spel) 
				if a ≥ -1 && a ≤ 11 && b ≥ 0 && b ≤ 8
					if spel[i] == "zet1x"
						p = a
						q = b+1
						d = tuple(p, q)
						c = tuple(a, b)
						f = tuple(c, d)
						if f ∈ E || f ∈ T
							println("This won't work, choose another move")
						else
							g(fill = "none", stroke = "red", stroke_width = "2") do
								line(x1 = join(["$((b+1)*l)", "cm"]), y1 = join(["$((a+2)*l)", "cm"]), x2 = join(["$((b+2)*l)", "cm"]), y2 = join(["$((a+2)*l)", "cm"]))
							end
							push!(C, c)
							e = tuple(c, d)
							push!(E, e)
							t = reverse(e)
							push!(T, t)
							b = b+1
						end
						
					elseif spel[i] == "zet2x"
						p = a
						q = b-1
						d = tuple(p, q)
						c = tuple(a, b)
						f = tuple(c, d)
						if f ∈ E || f ∈ T
							println("This won't work, choose another move")
						else
							g(fill = "none", stroke = "red", stroke_width = "2") do
								line(x1 = join(["$((b+1)*l)", "cm"]), y1 = join(["$((a+2)*l)", "cm"]), x2 = join(["$(b*l)", "cm"]), y2 = join(["$((a+2)*l)", "cm"]))
							end
							push!(C, c)
							e = tuple(c, d)
							push!(E, e)
							t = reverse(e)
							push!(T, t)
							b = b-1
						end
						
					elseif spel[i] == "zet3x"
						p = a-1
						q = b
						d = tuple(p, q)
						c = tuple(a, b)
						f = tuple(C, d)
						if f ∈ E || f ∈ T
							println("This won't work, choose another move")
						else
							g(fill = "none", stroke = "red", stroke_width = "2") do
								line(x1 = join(["$((b+1)*l)", "cm"]), y1 = join(["$((a+2)*l)", "cm"]), x2 = join(["$((b+1)*l)", "cm"]), y2 = join(["$((a+3)*l)", "cm"]))
							end
							push!(C, c)
							e = tuple(c, d)
							push!(E, e)
							t = reverse(e)
							push!(T, t)
							a = a+1
						end
						
					elseif spel[i] == "zet4x"
						p = a+1
						q = b
						d = tuple(p, q)
						c = tuple(a, b)
						f = tuple(c, d)
						if f ∈ E || f ∈ T
							println("This won't work, choose another move")
						else
							g(fill = "none", stroke = "red", stroke_width = "2") do
								line(x1 = join(["$((b+1)*l)", "cm"]), y1 = join(["$((a+2)*l)", "cm"]), x2 = join(["$((b+1)*l)", "cm"]), y2 = join(["$((a+1)*l)", "cm"]))
							end
							push!(C, c)
							e = tuple(c, d)
							push!(E, e)
							t = reverse(e)
							push!(T, t)
							a = a-1
						end
						
					elseif spel[i] == "zet5x"
						p = a+1
						q = b+1
						d = tuple(p, q)
						c = tuple(a, b)
						f = tuple(c, d)
						if f ∈ E || f ∈ T
							println("This won't work, choose another move")
						else
							g(fill = "none", stroke = "red", stroke_width = "2") do
								line(x1 = join(["$((b+1)*l)", "cm"]), y1 = join(["$((a+2)*l)", "cm"]), x2 = join(["$((b+2)*l)", "cm"]), y2 = join(["$((a+3)*l)", "cm"]))
							end
							push!(C, c)
							e = tuple(c, d)
							push!(E, e)
							t = reverse(e)
							push!(T, t)
							a = a+1
							b = b+1
						end
						
					elseif spel[i] == "zet6x"
						p = a-1
						q = b+1
						d = tuple(p, q)
						c = tuple(a, b)
						f = tuple(c, d)
						if f ∈ E || f ∈ T
							println("This won't work, choose another move")
						else
							g(fill = "none", stroke = "red", stroke_width = "2") do
								line(x1 = join(["$((b+1)*l)", "cm"]), y1 = join(["$((a+2)*l)", "cm"]), x2 = join(["$((b+2)*l)", "cm"]), y2 = join(["$((a+1)*l)", "cm"]))
							end
							push!(C, c)
							e = tuple(c, d)
							push!(E, e)
							t = reverse(e)
							push!(T, t)
							a = a-1
							b = b+1
						end
						
					elseif spel[i] == "zet7x"
						p = a+1
						q = b-1
						d = tuple(p, q)
						c = tuple(a, b)
						f = tuple(c, d)
						if f ∈ E || f ∈ T
							println("This won't work, choose another move")
						else
							g(fill = "none", stroke = "red", stroke_width = "2") do
								line(x1 = join(["$((b+1)*l)", "cm"]), y1 = join(["$((a+2)*l)", "cm"]), x2 = join(["$(b*l)", "cm"]), y2 = join(["$((a+3)*l)", "cm"]))
							end
							push!(C, c)
							e = tuple(c, d)
							push!(E, e)
							t = reverse(e)
							push!(T, t)
							a = a+1
							b = b-1
						end
						
					elseif spel[i] == "zet8x"
						p = a-1
						q = b-1
						d = tuple(p, q)
						c = tuple(a, b)
						f = tuple(c, d)
						if f ∈ E || f ∈ T
							println("This won't work, choose another move")
						else
							g(fill = "none", stroke = "red", stroke_width = "2") do
								line(x1 = join(["$((b+1)*l)", "cm"]), y1 = join(["$((a+2)*l)", "cm"]), x2 = join(["$(b*l)", "cm"]), y2 = join(["$((a+1)*l)", "cm"]))
							end
							push!(C, c)
							e = tuple(c, d)
							push!(E, e)
							t = reverse(e)
							push(T, t)
							a = a-1
							b = b-1
						end
							
					elseif spel[i] == "zet1y"
						p = a
						q = b+1
						d = tuple(p, q)
						c = tuple(a, b)
						f = tuple(c, d)
						if f ∈ E || f ∈ T
							println("This won't work, choose another move")
						else
							g(fill = "none", stroke = "green", stroke_width = "2") do
								line(x1 = join(["$((b+1)*l)", "cm"]), y1 = join(["$((a+2)*l)", "cm"]), x2 = join(["$((b+2)*l)", "cm"]), y2 = join(["$((a+2)*l)", "cm"]))
							end
							push!(C, c)
							e = tuple(c, d)
							push!(E, e)
							t = reverse(e)
							push!(T, t)
							b = b+1
						end
						
					elseif spel[i] == "zet2y"
						p = a
						q = b-1
						d = tuple(p, q)
						c = tuple(a, b)
						f = tuple(c, d)
						if f ∈ E || f ∈ T
							println("This won't work, choose another move")
						else
							g(fill = "none", stroke = "green", stroke_width = "2") do
								line(x1 = join(["$((b+1)*l)", "cm"]), y1 = join(["$((a+2)*l)", "cm"]), x2 = join(["$(b*l)", "cm"]), y2 = join(["$((a+2)*l)", "cm"]))
							end
							push!(C, c)
							e = tuple(c, d)
							push!(E, e)
							t = reverse(e)
							push!(T, t)
							b = b-1
						end
						
					elseif spel[i] == "zet3y"
						p = a-1
						q = b
						d = tuple(p, q)
						c = tuple(a, b)
						f = tuple(c, d)
						if f ∈ E || f ∈ T
							println("This won't work, choose another move")
						else
							g(fill = "none", stroke = "green", stroke_width = "2") do
								line(x1 = join(["$((b+1)*l)", "cm"]), y1 = join(["$((a+2)*l)", "cm"]), x2 = join(["$((b+1)*l)", "cm"]), y2 = join(["$((a+3)*l)", "cm"]))
							end
							push!(C, c)
							e = tuple(c, d)
							push!(E, d)
							t = reverse(e)
							push!(T, t)
							a = a+1
						end
						
					elseif spel[i] == "zet4y"
						p = a+1
						q = b
						d = tuple(p, q)
						c = tuple(a, b)
						f = tuple(c, d)
						if f ∈ E || f ∈ T
							println("This won't work, choose another move")
						else
							g(fill = "none", stroke = "green", stroke_width = "2") do
								line(x1 = join(["$((b+1)*l)", "cm"]), y1 = join(["$((a+2)*l)", "cm"]), x2 = join(["$((b+1)*l)", "cm"]), y2 = join(["$((a+1)*l)", "cm"]))
							end
							push!(C, c)
							e = tuple(c, d)
							push!(E, e)
							t = reverse(e)
							push!(T, t)
							a = a-1
						end
						
					elseif spel[i] == "zet5y"
						p = a+1
						q = b+1
						d = tuple(p, q)
						c = tuple(a, b)
						f = tuple(c, d)
						if f ∈ E || f ∈ T
							println("This won't work, choose another move")
						else
							g(fill = "none", stroke = "green", stroke_width = "2") do
								line(x1 = join(["$((b+1)*l)", "cm"]), y1 = join(["$((a+2)*l)", "cm"]), x2 = join(["$((b+2)*l)", "cm"]), y2 = join(["$((a+3)*l)", "cm"]))
							end
							push!(C, c)
							e = tuple(c, d)
							push!(E, e)
							t = reverse(e)
							push!(T, t)
							a = a+1
							b = b+1
						end
						
					elseif spel[i] == "zet6y"
						p = a-1
						q = b+1
						d = tuple(p, q)
						c = tuple(a, b)
						f = tuple(c, d)
						if f ∈ E || f ∈ T
							println("This won't work, choose another move")
						else
							g(fill = "none", stroke = "green", stroke_width = "2") do
								line(x1 = join(["$((b+1)*l)", "cm"]), y1 = join(["$((a+2)*l)", "cm"]), x2 = join(["$((b+2)*l)", "cm"]), y2 = join(["$((a+1)*l)", "cm"]))
							end
							push!(C, c)
							e = tuple(c, d)
							push!(E, e)
							t = reverse(e)
							push(T, t)
							a = a-1
							b = b+1
						end
						
					elseif spel[i] == "zet7y"
						p = a+1
						q = b-1
						d = tuple(p, q)
						c = tuple(a, b)
						f = tuple(c, d)
						if f ∈ E || f ∈ T
							println("This won't work, choose another move")
						else
							g(fill = "none", stroke = "green", stroke_width = "2") do
								line(x1 = join(["$((b+1)*l)", "cm"]), y1 = join(["$((a+2)*l)", "cm"]), x2 = join(["$(b*l)", "cm"]), y2 = join(["$((a+3)*l)", "cm"]))
							end
							push!(C, c)
							e = tuple(c, d)
							push!(E, e)
							t = reverse(e)
							push!(T, t)
							a = a+1
							b = b-1
						end
							
					elseif spel[i] == "zet8y"
						p = a-1
						q = b-1
						d = tuple(p, q)
						c = tuple(a, b)
						f = tuple(c, d)
						if f ∈ E || f ∈ T
							println("This won't work, choose another move")
						else
							g(fill = "none", stroke = "green", stroke_width = "2") do
								line(x1 = join(["$((b+1)*l)", "cm"]), y1 = join(["$((a+2)*l)", "cm"]), x2 = join(["$(b*l)", "cm"]), y2 = join(["$((a+1)*l)", "cm"]))
							end
							push!(C, c)
							e = tuple(c, d)
							push!(E, e)
							t = reverse(e)
							push!(T, t)
							a = a-1
							b = b-1
						end
					end
				end
				if (a, b) ∈ C
					println("De speler die aan zet is, mag nog een zet doen")
				end
				if b == 0 || b == 8
					println("Het spel is gedaan")
					for i in 1:length(C)
						pop!(C)
					end
					for i in 1:length(spel)
					pop!(spel)
					end
					(a, b) = (5, 4)
				end
				for i in 3:5
					if (a, b) == (-1, i) || (a, b) == (11, i)
						println("Het spel is gedaan")
						for i in 1:length(C)
							pop!(C)
						end
						for i in 1:length(spel)
							pop!(spel)
						end
						(a, b) = (5, 4)
					end
				end
				for i in [0, 1, 2, 6, 7, 8]
					if (a, b) == (0, i) || (a, b) == (10, i)
						println("Het spel is gedaan")
						for i in 1:length(C)
							pop!(C)
						end
						for i in 1:length(spel)
							pop!(spel)
						end
						(a, b) = (5, 4)
					end
				end	
			end
		end
	end
end~~~
1 Like

Welcome to our community! :confetti_ball:

We will need the code to be able to help more. However, I see that the problem happened at Main.workspace289 are you using Pluto.jl or something that wraps your code in automatically generated modules?

2 Likes

Thanks for your answer! I am indeed using Pluto.jl to write my code + NativeSVG if that’s necessary information. I will try to upload my code.

My code might look a bit unclear, so here’s where I think something might have gone wrong.

			for i in 1:length(spel) 
				if a ≥ -1 && a ≤ 11 && b ≥ 0 && b ≤ 8
					if spel[i] == "zet1x"
						p = a
						q = b+1
						d = tuple(p, q)
						c = tuple(a, b)
						f = tuple(c, d)
						if f ∈ E || f ∈ T
							println("This won't work, choose another move")
						else
							g(fill = "none", stroke = "red", stroke_width = "2") do
								line(x1 = join(["$((b+1)*l)", "cm"]), y1 = join(["$((a+2)*l)", "cm"]), x2 = join(["$((b+2)*l)", "cm"]), y2 = join(["$((a+2)*l)", "cm"]))
							end
							push!(C, c)
							e = tuple(c, d)
							push!(E, e)
							t = reverse(e)
							push!(T, t)
							b = b+1
						end

For your interest: Previously, I defined:
(a, b) = (5, 4)
C =
E =
T =
C, E and T are empty arrays
There’s a variable “keuze” that takes a string “zet1x”. The function “input” takes the value of “keuze” and puts it in an array “spel”. My code is always evaluating that array “spel”. I defined those arrays C, E and T to save the coordinates we’ve passed, this is to be able to add the rules to the game. The weird thing is that my code used to work untill I added the array T. So without T and t, my code used to work.

To add some extra information:
If I define my a and b at the beginning of my code, I get the error. Like I did here:

While if I define my a and b later in my code, I don’t get the error. Even though it’s on the same Hight. Like I did here:

I think the first step here would be to paste your code in a way we can run in our machines. I realize this could be a bit more borhersome, but it will allow us to help you more quickly (and, we really want to since we’re a nice community! :slight_smile: )

For example

your code should be inside this 

Some other useful guidelines on how to post code, use discourse most efficiently etc. are also given here:

1 Like

Thanks for your advice, I hope it’s better now.

Looks like a scope problem. Try to add global a just before a is first used.

(We still cannot copy/paste and run the code)