Alice Community  

Go Back   Alice Community > Alice 2 > How do I...?

Reply
 
Thread Tools Display Modes
Can't get program to return a false value!
Old
fallout87
Junior Member
 
Status: Offline
Posts: 11
Join Date: Oct 2011
Location: Small Country Town
Default Can't get program to return a false value! - 10-17-2011, 07:06 AM

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.

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

Last edited by fallout87; 10-17-2011 at 01:20 PM.
   
Reply With Quote
Old
x2495iiii
Super Moderator
 
x2495iiii's Avatar
 
Status: Offline
Posts: 3,508
Join Date: Dec 2008
Location: Somewhere in the Continental U.S.
Default 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.
   
Reply With Quote
Old
fallout87
Junior Member
 
Status: Offline
Posts: 11
Join Date: Oct 2011
Location: Small Country Town
Default 10-17-2011, 01:22 PM

Here's the alice file
Attached Files
File Type: a2w pracLesson04MagicSquareVersion2.a2w (153.4 KB, 9 views)

Last edited by fallout87; 10-17-2011 at 03:28 PM.
   
Reply With Quote
Old
fallout87
Junior Member
 
Status: Offline
Posts: 11
Join Date: Oct 2011
Location: Small Country Town
Default 10-17-2011, 03:43 PM

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

Last edited by fallout87; 10-17-2011 at 03:49 PM.
   
Reply With Quote
Old
x2495iiii
Super Moderator
 
x2495iiii's Avatar
 
Status: Offline
Posts: 3,508
Join Date: Dec 2008
Location: Somewhere in the Continental U.S.
Default 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.
   
Reply With Quote
Old
fallout87
Junior Member
 
Status: Offline
Posts: 11
Join Date: Oct 2011
Location: Small Country Town
Default 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 : )
   
Reply With Quote
Old
x2495iiii
Super Moderator
 
x2495iiii's Avatar
 
Status: Offline
Posts: 3,508
Join Date: Dec 2008
Location: Somewhere in the Continental U.S.
Default 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?
   
Reply With Quote
Old
fallout87
Junior Member
 
Status: Offline
Posts: 11
Join Date: Oct 2011
Location: Small Country Town
Default 10-18-2011, 06:32 PM

Quote:
Originally Posted by x2495iiii View Post
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.

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
   
Reply With Quote
Old
x2495iiii
Super Moderator
 
x2495iiii's Avatar
 
Status: Offline
Posts: 3,508
Join Date: Dec 2008
Location: Somewhere in the Continental U.S.
Default 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.
   
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Copyright ©2024, Carnegie Mellon University
Alice 2.x © 1999-2012, Alice 3.x © 2008-2012, Carnegie Mellon University. All rights reserved.