1

I'm trying to make a math addition program, and I have a few questions.

  1. How do I make it so when it asks the question, it says it on the same dialog box? Such as: "What is the answer of x and y" instead of "what is the answer of x" followed by a new dialog "and y"?
  2. It returns nothing when you type the correct answer in. I want it so when you type in the correct answer it displays "correct" or "Goodjob!", and when you get it wrong it says "Incorrect" or "Try Again".

Code:

set num1 to (random number from 0 to 100)
set num2 to (random number from 0 to 100)
set answer to num1 + num2
display dialog "What is the answer of " & num1
if text returned of (display dialog "and " & num2 default answer "") = answer then display dialog "correct"

1 Answer 1

1

A few things:

  1. There should be two dialogs that get displayed:
    • One for the question, which should be just one string of text. This will have the default answer.
    • One for the result
  2. It is not returning/displaying anything because all the answers are "incorrect" and incorrect answers are not triggering anything. To fix this:
    • You will need to convert the user input text returned to a number for comparison with answer
    • You will have to catch incorrect answers using else to display the "incorrect" dialog.

The result:

set num1 to (random number from 0 to 100)
set num2 to (random number from 0 to 100)
set answer to num1 + num2
set question to ("What is the answer of " & num1 & " and " & num2)
set dialog to (display dialog question default answer "")
if text returned of dialog as number = answer then
    display dialog "correct"
else
    display dialog "incorrect"
end if

Furthermore, if you wish to allow the user to re-try on incorrect attempts, you will need to use a loop that ends on a correct attempt.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .