1
$\begingroup$

Can we write code in Mathematica for the game where the user guesses an integer between 1-99 produced by the computer? If you have any ideas, I would be glad if you could help. I tried this:

number = RandomInteger[{1, 99}]; 
While[guess =!= number, guess = Input["Guess my number"]]; 
Print["Well guessed!"] 
$\endgroup$
4
  • $\begingroup$ You could start your program by saving this into a variable: RandomInteger[{1, 99}] $\endgroup$ Apr 26, 2021 at 20:19
  • $\begingroup$ THANKS. I tried this but the system stays running. For example, I can find between 1-10 immediately, but as the range gets bigger, 1-100, the more I get errors from the system. Can we get this before? $\endgroup$
    – seda
    Apr 26, 2021 at 20:58
  • 1
    $\begingroup$ Please show the code you are running -- that will help us to help you. $\endgroup$
    – bill s
    Apr 26, 2021 at 21:19
  • $\begingroup$ number = RandomInteger[{1, 99}]; While[guess =!= number, guess = Input["Guess my number"]]; Print["Well guessed!"] $\endgroup$
    – seda
    Apr 26, 2021 at 21:21

1 Answer 1

1
$\begingroup$

Here you have a minimal solution to get you started.

DynamicModule[{n, guess = 0}, 
    {
     InputField[Dynamic[guess], Number], 
     Dynamic[If[n == guess, "You won", "Try again"]],
     Dynamic[n] (* for debugging *)
    }, 
 Initialization :> (n = RandomInteger[{1, 99}])]

Type a number, press tab, see if you win.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.