can someone please help me with this problem?

When branching to a subroutine, variables do not retain their value------- I read somewhere that “properties” are the way to solve this problem?

I have a script which has randomly generated a number (0-32).

based off of that number, it goes to one of 33 subroutines…

Each subroutine asks a specific question…

… now… I am trying to make it, so that if you get the question right, it will generate a new random number and begin the process over again-- and if you get it wrong, it will ask you the same question over and over…

So, how do I effectively accomplish this? How do I tell it from within a subroutine, if it is right – pick a new number and remember it…


You can use also a global:

global n

on run
	set n to random number from 1 to 5
end run

to blah()
	display dialog "current number is " & n
	set n to random number from 1 to 5
end blah


Here’s a novel example of calling random suroutines by index:

property was_true : true
global n

set subs to {a, b, c}
if was_true then
set n to (random number 2) + 1
end if
set h to (item n of subs)
set was_true to h() as boolean
display dialog ("Result was " & was_true & “!”)

on a()
return (random number 1)
end a
on b()
return (random number 1)
end b
on c()
return (random number 1)
end c

Usually, you want your subroutines to be modular. You can send it different parameters. Something like this:

property qa_list : {{“What is your name?”, “Joe”} ¬
, {“What is your favorite color?”, “green”}}
property was_correct : true
global n

if was_correct then
set n to (random number 1) + 1
end if
set {q, a} to item n of qa_list
set was_correct to AskQuestion(q, a)
display dialog (was_correct as string)

on AskQuestion(q, a)
set tr to “”
display dialog q default answer a
set tr to text returned of result
end try
set is_correct to tr=a
return is_correct
end AskQuestion