Alice Community

Alice Community (http://www.alice.org/community/index.php)
-   The Lounge (http://www.alice.org/community/forumdisplay.php?f=4)
-   -   Nested If/Else? (http://www.alice.org/community/showthread.php?t=12020)

jkidd 06-25-2018 03:19 PM

Nested If/Else?
 
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: [URL="https://image.ibb.co/hyYpuo/ifelse.jpg"]https://image.ibb.co/hyYpuo/ifelse.jpg[/URL]

Any help or advice would be great!

Thanks.

chickentree 06-25-2018 04:29 PM

[QUOTE=jkidd;58087]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: [URL="https://image.ibb.co/hyYpuo/ifelse.jpg"]https://image.ibb.co/hyYpuo/ifelse.jpg[/URL]

Any help or advice would be great!

Thanks.[/QUOTE]

You have two problems with your code:[LIST=1][*]age is set to 21 and never changed.[*]The if statements need to be nested.[/LIST]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
[/CODE]

Hope this helps

jkidd 06-25-2018 05:11 PM

Hi chickentree -

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

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.

chickentree 06-25-2018 07:58 PM

[QUOTE=jkidd;58089]Hi chickentree -

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

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.[/QUOTE]

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

jkidd 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

chickentree 06-26-2018 12:32 PM

1 Attachment(s)
[QUOTE=jkidd;58091]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[/QUOTE]

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

jkidd 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

chickentree 06-26-2018 06:06 PM

[QUOTE=jkidd;58093]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[/QUOTE]

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


All times are GMT -5. The time now is 01:43 PM.

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