Alice Community  

Go Back   Alice Community > General Discussion > The Lounge

Reply
 
Thread Tools Display Modes
Nested If/Else?
Old
jkidd
Member
 
Status: Offline
Posts: 21
Join Date: Jun 2018
Default Nested If/Else? - 06-25-2018, 03:19 PM

Hey guys -

I am working on an assignment that requires me to have a bouncer ask for the user's age. If the user is below 21, they don't get in. If the user is above 21, they're too old. If the user is indeed 21 years old, they get into the bar.

I have tried every combo that I can think of, and have read the class textbook, but can't make it work. Click on the following link to see a screenshot of my method in Alice: https://image.ibb.co/hyYpuo/ifelse.jpg

Any help or advice would be great!

Thanks.
   
Reply With Quote
Old
chickentree
Super Moderator
 
Status: Offline
Posts: 250
Join Date: Dec 2012
Location: Frosno, Ca
Default 06-25-2018, 04:29 PM

Quote:
Originally Posted by jkidd View Post
Hey guys -

I am working on an assignment that requires me to have a bouncer ask for the user's age. If the user is below 21, they don't get in. If the user is above 21, they're too old. If the user is indeed 21 years old, they get into the bar.

I have tried every combo that I can think of, and have read the class textbook, but can't make it work. Click on the following link to see a screenshot of my method in Alice: https://image.ibb.co/hyYpuo/ifelse.jpg

Any help or advice would be great!

Thanks.
You have two problems with your code:
  1. age is set to 21 and never changed.
  2. The if statements need to be nested.
What you need is to first set Age to the users input and then put one if statement inside the other
Code:
Age=0
    Set Age to ask user for a number
    If Age < 21 Then
        // do too young stuff
    else
        If Age > 21 Then
            //do too old stuff
        else
            // The user is not too young or too old
Hope this helps


Mark Henwood
mhenwood@ieee.org
   
Reply With Quote
Old
jkidd
Member
 
Status: Offline
Posts: 21
Join Date: Jun 2018
Default 06-25-2018, 05:11 PM

Hi chickentree -

I did everything that you suggested, as you can tell from this screenshot: https://image.ibb.co/nMUhzo/ifelse_2.jpg

If I enter a number under 21, the bouncer responds accordingly. If I enter the number 21 or a number over 21, the bouncer says "welcome to our exclusive club". If the user types in a number over 21, the bouncer is supposed to say that the user is too old.

I'm not sure what I'm doing wrong.
   
Reply With Quote
Old
chickentree
Super Moderator
 
Status: Offline
Posts: 250
Join Date: Dec 2012
Location: Frosno, Ca
Default 06-25-2018, 07:58 PM

Quote:
Originally Posted by jkidd View Post
Hi chickentree -

I did everything that you suggested, as you can tell from this screenshot: https://image.ibb.co/nMUhzo/ifelse_2.jpg

If I enter a number under 21, the bouncer responds accordingly. If I enter the number 21 or a number over 21, the bouncer says "welcome to our exclusive club". If the user types in a number over 21, the bouncer is supposed to say that the user is too old.

I'm not sure what I'm doing wrong.
You are still not saving the user input in the age variable. This needs to be done before the if statement, please look at my example code again.

As it is now the user input is tested to see if it is less than 21, after that the user answer is thrown away and the value of Age ( which is now 0) is used in the enclosed if statement so it will never be more than 21.


Mark


Mark Henwood
mhenwood@ieee.org
   
Reply With Quote
Old
jkidd
Member
 
Status: Offline
Posts: 21
Join Date: Jun 2018
Default 06-25-2018, 09:09 PM

Hi chickentree -

I am at a loss as to what you mean by "saving the user input in the age variable".

I have the Alice program asking the user for a number. I've looked over your code many times, and I'm just not understanding.

:/

- jkidd
   
Reply With Quote
Old
chickentree
Super Moderator
 
Status: Offline
Posts: 250
Join Date: Dec 2012
Location: Frosno, Ca
Default 06-26-2018, 12:32 PM

Quote:
Originally Posted by jkidd View Post
Hi chickentree -

I am at a loss as to what you mean by "saving the user input in the age variable".

I have the Alice program asking the user for a number. I've looked over your code many times, and I'm just not understanding.

:/

- jkidd
I have attached an example program. Please compare this with yours to make sure you understand what is going on and why your Age value wasn't getting set correctly.

Mark
Attached Files
File Type: a2w CheckAge.a2w (643.1 KB, 1 views)


Mark Henwood
mhenwood@ieee.org
   
Reply With Quote
Old
jkidd
Member
 
Status: Offline
Posts: 21
Join Date: Jun 2018
Default 06-26-2018, 01:38 PM

Hi chickentree -

Thank you for your example. That really helped me out, as I'm more of a visual learner.

Everything else made sense, except for this last part. Your advice is much appreciated.

- jkidd
   
Reply With Quote
Old
chickentree
Super Moderator
 
Status: Offline
Posts: 250
Join Date: Dec 2012
Location: Frosno, Ca
Default 06-26-2018, 06:06 PM

Quote:
Originally Posted by jkidd View Post
Hi chickentree -

Thank you for your example. That really helped me out, as I'm more of a visual learner.

Everything else made sense, except for this last part. Your advice is much appreciated.

- jkidd
As long as you understand how this works and why it didn't originally Im happy.
Mark


Mark Henwood
mhenwood@ieee.org
   
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 ©2019, Carnegie Mellon University
Alice 2.x 1999-2012, Alice 3.x 2008-2012, Carnegie Mellon University. All rights reserved.