 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
rgblair11 Guest
|
Posted: Fri Aug 13, 2004 9:53 am Post subject: If Else Confusion |
|
|
I'm trying to learn about program flow by creating a simple "guess the number game" (i.e. define a random number, have user guess, display higer or lower).
Here is the code I'm starting with. Problem is, no mater what the guess is, MsgBox Higher displays and then the "guess" dialog starts again. I realize I'm doing something complety wrong, but I can't figure it out. Here is the code:
InputBox, min, Enter Minimum Value, Enter Minimum Value,
Inputbox, max, Enter Maximum Value, Enter Maximum Value
random, num, %min%, %max%
MsgBox, The Number is %num% ; used for testing, I want to know what the number is
GUESS:
InputBox, guess, Guess, Enter Guess Between %min% and %max%
If %guess% < %num%
{
MsgBox, Higher
Gosub, GUESS
}
else if %guess% > %num%
{
MsgBox, Lower
Gosub, Guess
} |
|
| Back to top |
|
 |
jordis
Joined: 30 Jul 2004 Posts: 78
|
Posted: Fri Aug 13, 2004 10:10 am Post subject: |
|
|
rgblair11,
the problem is in these lines:
If %guess% < %num%
else if %guess% > %num%
you should get rid of the %s in guess, like:
If guess < %num%
else if guess > %num%
jordi |
|
| Back to top |
|
 |
BoBo Guest
|
Posted: Fri Aug 13, 2004 10:52 am Post subject: |
|
|
IfEqual, var, value
same
if var = value
taken from the help.
As I know you use the percentage char in cases where literals could be used as well e.g. with the Send command
| Code: | Send, Myname // This will send the string "Myname"
MyName = rgblair11
Send, %Myname% // That will send the variables content "rgblair11" |
|
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10467
|
Posted: Fri Aug 13, 2004 11:04 am Post subject: |
|
|
Also, you should not do "Gosub Guess" in this case. You should either use "Goto Guess" or enclose the contents in a loop as shown below:
| Code: | InputBox, min, Enter Minimum Value, Enter Minimum Value,
Inputbox, max, Enter Maximum Value, Enter Maximum Value
random, num, %min%, %max%
MsgBox, The Number is %num% ; used for testing, I want to know what the number is
Loop
{
InputBox, guess, Guess, Enter Guess Between %min% and %max%
If guess < %num%
MsgBox, Higher
else if guess > %num%
MsgBox, Lower
else ; Answer is correct, so break out of the loop.
break
}
MsgBox You guessed it!
|
|
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|