A few questions - variable variable names & a problem

  1. Is it possible to have variable names that depend on other variables? EX:

set word1 to "a"
set word2 to "b"
set word3 to "c"
set ex_var to random number between 1 and 3
say ("word" & exvar)
  1. IF NOT, this script is being weird. It is giving me the same pattern: Give the same word twice, say you got a word you didn’t even get yet was correct, then give another word twice. WHY?
set helpwords to "ostentatious candidate facetious"
set helpword1 to first word of helpwords
set helpword2 to second word of helpwords
set helpword3 to third word of helpwords
repeat 3 times
	set question_num to random number from 1 to 3
	if question_num is 1 then
		set answer1 to "wrong"
		repeat until answer1 is helpword1
			say helpword1
			set ask1 to (display dialog "Spell this word" default answer "" buttons {"Repeat Word", "Continue"} default button 2)
			set button1 to button returned of ask1
			set answer1 to text returned of ask1
		end repeat
		display dialog "CORRECT! The answer was " & helpword1
	end if
	if question_num is 2 then
		set answer2 to "wrong"
		repeat until answer2 is helpword2
			say helpword2
			set ask2 to (display dialog "Spell this word" default answer "" buttons {"Repeat Word", "Continue"} default button 2)
			set button2 to button returned of ask2
			set answer2 to text returned of ask2
		end repeat
		display dialog "CORRECT! The answer was " & helpword2
	end if
	if question_num is 3 then
		set answer3 to "wrong"
		repeat until answer3 is helpword3
			say helpword3
			set ask3 to (display dialog "Spell this word" default answer "" buttons {"Repeat Word", "Continue"} default button 2)
			set button3 to button returned of ask3
			set answer3 to text returned of ask3
		end repeat
	end if
	display dialog "CORRECT! The answer was " & helpword3
end repeat

No.

If you ask for a random number from 1 to 3 three times in a row, the odds are much greater that one will be repeated than that all three will be returned.

Hi,

your “variable problem” can be accomplished quite easy with a list


set helpwords to {"ostentatious", "candidate", "facetious"}
repeat 3 times
	set question_num to random number from 1 to 3
	set answer to "wrong"
	set helpword to (item question_num of helpwords)
	repeat until answer is helpword
		say helpword
		set {text returned:answer, button returned:button} to (display dialog "Spell this word" default answer "" buttons {"Cancel", "Repeat Word", "Continue"} default button 3)
	end repeat
	display dialog "CORRECT! The answer was " & helpword
end repeat


PS: this script uses a list of random numbers, so each helpword is processed once


property helpwords : {"ostentatious", "candidate", "facetious"}

set numberOfHelpwords to count helpwords
set randomNumberList to createRandomNumberList(numberOfHelpwords)
repeat with question_num from 1 to numberOfHelpwords
	set answer to "wrong"
	set helpword to (item question_num of helpwords)
	repeat until answer is helpword
		say helpword
		set {text returned:answer, button returned:button} to (display dialog "Spell this word" default answer "" buttons {"Cancel", "Repeat Word", "Continue"} default button 3)
	end repeat
	display dialog "CORRECT! The answer was " & helpword
end repeat

on createRandomNumberList(max)
	set randomList to {}
	repeat until (count randomList) is max
		set randomNumber to random number from 1 to max
		if randomList does not contain randomNumber then set end of randomList to randomNumber
	end repeat
	return randomList
end createRandomNumberList

Same as Stefan’s but without keeping track of random number list but still is random and every item is used once as well.

set theWordList to {"ostentatious", "candidate", "facetious"}
set theAnswer to missing value
repeat
	set helpWordItem to random number from 1 to theWordList's length
	repeat until theAnswer is item helpWordItem of theWordList
		say item helpWordItem of theWordList
		set theAnswer to text returned of (display dialog "Spell this word" default answer "" buttons {"Cancel", "Repeat Word", "Continue"} default button 3)
	end repeat
	display dialog "CORRECT! The answer was " & item helpWordItem of theWordList
	if theWordList's length = 1 then
		exit repeat
	else
		set item helpWordItem of theWordList to missing value
		set theWordList to every text of theWordList
	end if
end repeat]

Edit: cleaned the code up a bit to make it more human readable.

And another variation for interest:


property helpwords : {"ostentatious", "candidate", "facetious"}

set numberOfHelpwords to count helpwords
set indexList to {}
repeat with n from 1 to numberOfHelpwords
	set end of indexList to n
end repeat

repeat numberOfHelpwords times
	set question_num to some integer of indexList
	set item question_num of indexList to missing value
	set answer to "wrong"
	set helpword to (item question_num of helpwords)
	repeat until answer is helpword
		say helpword
		set {text returned:answer, button returned:button} to (display dialog "Spell this word" default answer "" buttons {"Cancel", "Repeat Word", "Continue"} default button 3)
	end repeat
	display dialog "CORRECT! The answer was " & helpword
end repeat

Edit: Or indeed a similar variation on DJ’s script:


set theWordList to {"ostentatious", "candidate", "facetious"}
set theAnswer to missing value

set wordsLeft to (count theWordList)
repeat wordsLeft times
	set helpWordIndex to random number from 1 to wordsLeft
	set helpword to text helpWordIndex of theWordList
	repeat until theAnswer is helpword
		say helpword
		set theAnswer to text returned of (display dialog "Spell this word" default answer "" buttons {"Cancel", "Repeat Word", "Continue"} default button 3)
	end repeat
	display dialog "CORRECT! The answer was " & helpword
	set text helpWordIndex of theWordList to missing value
	set wordsLeft to wordsLeft - 1
end repeat

Further edit: Corrected an oversight in the second script which was causing it to enthuse that the correct answer was missing value.

If you change


...
set wordsLeft to (count theWordList)
...

to


set wordsLeft to (count of every text of theWordList)

Then it doesn’t matter if there are other items in the list of words as well like integers, booleans or records for example. So it’s even more idiot-proof :smiley: