repeating code--not working

I am new to coding and trying to do something relatively simple. I would like to make a game where random numbers are displayed. If the correct number is typed in, then I would like a new number to be shown. If the number typed in is wrong, I’d like the player to have to try again. I would like this process to repeat 6 times. Could someone explain what I’m doing wrong?

Also, eventually I would like to replace the dialog box with pictures that are clicked. I assume that I need to make an array delineating the pictures as variables and enable these pictures to receive clicks in xcode’s interface builder?

Thanks so much!

on run
set c to 0
try
repeat
play()
feedback()
set c to c + 1
if c = 6 then exit repeat
end repeat
end try
end run

on play()

set a to random number from 1 to 5
set r to run script (a as text)
set u to text returned of (display dialog (a as text) default answer "")

end play

on feedback()
repeat
if (u as string) is not (r as string) then
set u to text returned of (display dialog (“Try again” & space & a as text) default answer “”)
else
display dialog “Well done!” buttons {“Cancel”, “Continue”} cancel button 1 default button 2
end if
end repeat
end feedback

Your functions aren’t returning anything. You are making some assumptions about the scope of variables U and R that just don’t work, unless you declare them as global, which might be overkill here.

This works but gives a repeating loop once answered correctly. I could fix it, but it might be helpful for you to take a look and understand why that’s happening now. :wink:

Chris

on run
    set c to 0
    try
        repeat
            set theAnswer to play()
            feedback(theAnswer)
            set c to c + 1
            if c = 6 then exit repeat
        end repeat
    end try
end run

on play()
    set a to random number from 1 to 5
    set r to (a as text)
    set u to text returned of (display dialog r default answer "")
    return {u, r}
end play

on feedback(inputValue)
    set u to item 1 of inputValue
    set r to item 2 of inputValue
    repeat
        if (u as string) is not (r as string) then
            set u to text returned of (display dialog ("Try again" & space & (r as string)) default answer "")
        else
            display dialog "Well done!" buttons {"Cancel", "Continue"} cancel button 1 default button 2
        end if
    end repeat
end feedback

hint: you might have to get the value of a button to exit…

Thanks. I think I have that part worked out, but now it doesn’t progress to show another number. I can’t seem to figure it out!

on run
set c to 0
try
repeat
set theAnswer to play()
feedback(theAnswer)
set c to c + 1
if c = 6 then exit repeat
end repeat
end try
end run

on play()
set a to random number from 1 to 5
set r to (a as text)
set u to text returned of (display dialog r default answer “”)
return {u, r}
end play

on feedback(inputValue)
set u to item 1 of inputValue
set r to item 2 of inputValue
repeat
if (u as string) is not (r as string) then
set u to text returned of (display dialog (“Try again” & space & (r as string)) default answer “”)
else
display dialog “Well done!” buttons {“Cancel”, “Continue”} cancel button 1 default button 2
if the button returned of the result is “Continue” then

		else
			exit repeat
		end if
	end if
end repeat

end feedback

Hi. Here is an alternatively structured version, for learning purposes. I took out a couple things; the incremental counter at the beginning of the script can be a specified count, and the explicit “return” in the handlers isn’t needed, since the implied value is it. I also chose to nest the handler calls.

repeat 2 times
	feedback(play(randNum()))
end repeat


on randNum()
	random number from 1 to 5
end randNum

on play(aNum)
	{(display dialog (aNum) default answer "")'s text returned} & aNum
end play

on feedback({this, |that|})
	repeat
		if not this = |that| as string then
			display dialog "Try again" buttons {"OK"} default button 1
			feedback(play(randNum()))
			exit repeat
		else
			display dialog "Well done!" buttons {"Cancel", "Continue"} cancel button 1 default button 2
			exit repeat
		end if
	end repeat
end feedback

Thanks! That makes a ton of sense. Now all I need to do is figure out how to link that to pictures so the kids can click on a picture and get feedback…I’ll keep chugging along!

Thanks!