Alice Community (http://www.alice.org/community/index.php)
-   How do I...? (http://www.alice.org/community/forumdisplay.php?f=16)
-   -   Can't get program to return a false value! (http://www.alice.org/community/showthread.php?t=7401)

 fallout87 10-17-2011 07:06 AM

Can't get program to return a false value!

So this is what I got (See attached screenshots)

Can not for the love of god work out why it won't return a false value.

Will return "Oh Yea. It's a Magic Square" for everything I put in the boxes for the values.

[B][I]EDIT - How do I get the attachment to come up "big" so you can see it properly???[/I][/B]

 x2495iiii 10-17-2011 09:18 AM

As you said, I can't really see it properly, but i'm guessing the variable is method level and not world level. Try making the variables attached to the world itself (global variables).

Also, you can attach .a2w files to forum posts. Do that next time you have trouble, or now if the global variable thing doesn't work.

 fallout87 10-17-2011 01:22 PM

1 Attachment(s)
Here's the alice file

 fallout87 10-17-2011 03:43 PM

My variables for the user number input are world properties. Does that make them global variables?

 x2495iiii 10-17-2011 07:31 PM

Yes it does. That's not the problem.

The problem is that the function's final bit of logic (if 1 == 2 == 3 etc.) doesn't work. You need to test out all the sums individually, not all at once.

There are two ways to do this. One is to have a series of nested if/else if statements which compare each sum individually. This is the hard, sloppy way.

The other is to do the following:
-use a number list that inserts the value of each sum at its end
-use two number variables, one set to the value of the first item in the list, the other set to zero
-use an if/else inside a for all in order loop to check each number in the list against the first number variable. If they match, increment the zero variable by 1
-at the end, check to see if the zero variable now equals 8. If so, all numbers match and the square is magic

Try the latter method first.

 fallout87 10-17-2011 11:03 PM

got it working

Added "and" == to the if statement between each variable in the function.

Also added a 'result' variable where it sets to true if the if statement is true and false if if statement is false, then returns 'result' variable at the end of the boolean function.

Thanks for the advice anyway : )

 x2495iiii 10-18-2011 10:55 AM

Nice. Wouldn't that allow for certain combinations of almost magic square numbers to be labeled as magic though, since not all sums are compared to each other?

 fallout87 10-18-2011 06:32 PM

[QUOTE=x2495iiii;42503]Yes it does. That's not the problem.

The problem is that the function's final bit of logic (if 1 == 2 == 3 etc.) doesn't work. You need to test out all the sums individually, not all at once.

There are two ways to do this. One is to have a series of nested if/else if statements which compare each sum individually. This is the hard, sloppy way.

The other is to do the following:
-use a number list that inserts the value of each sum at its end
-use two number variables, one set to the value of the first item in the list, the other set to zero
-use an if/else inside a for all in order loop to check each number in the list against the first number variable. If they match, increment the zero variable by 1
-at the end, check to see if the zero variable now equals 8. If so, all numbers match and the square is magic

Try the latter method first.[/QUOTE]

Number lists? Are you talking about arrays? If so I don't think I can use them as we haven't covered that yet : ( and don't think they are expected.

The way I did it seems to work but if you think it will return un - magic number squares then I will have to review the exercise. (Can't have that happening) lol

 x2495iiii 10-18-2011 07:35 PM

Your method will only sometimes work. In order to seal all the holes, you'd need to expand the if/else conditional to include all the numbers. If you expand the conditional to have the first sum compared to the other 7 sums (should be a total of 8 comparisons, 7 "and" joiners), it should catch all the un-magic squares. The list is just a neater way to do it, but it's not necessary.

 All times are GMT -5. The time now is 10:31 AM.