Alice Community  

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

Reply
 
Thread Tools Display Modes
Create a change-counting game
Old
exographicskip
Junior Member
 
Status: Offline
Posts: 3
Join Date: Mar 2014
Default Create a change-counting game - 03-03-2014, 07:33 PM

How do I get the user to enter arbitrary combinations of coins that will be recognized as a dollar?

I am attempting to have a character ask the user to enter a number of pennies, nickels, dimes, and quarters. If the total value of the coins entered equals a dollar, the character congratulates the user. Otherwise, the character tells the user whether the amount entered was more or less than one dollar (see: Starting Out with Alice, 3/E Exercise 4).

I have created variables for the dollar and coins, one variable per each (e.g., dollar = 1, quarter = 0.25). There is an If/Else statement with the true/false actions.

My handiwork:


EDIT: If anyone can tell me how to crop the image on the forum, I will gladly do so!
Attached Files
File Type: a2w Money 2.5.a2w (227.6 KB, 0 views)

Last edited by exographicskip; 03-03-2014 at 07:34 PM. Reason: Ugly looking formatting (lackthereof) for the image.
   
Reply With Quote
The basic idea
Old
chickentree
Super Moderator
 
Status: Offline
Posts: 250
Join Date: Dec 2012
Location: Frosno, Ca
Default The basic idea - 03-04-2014, 07:55 AM

There are a couple of problems here.
The first one is that you are asking one question and expecting 4 answers. The simplest solution is to ask 4 questions. So the interaction might go something like:
Code:
rabbit say "Can you please give me exactly one dollar in change?"
rabbit ask "How many quarters do you have?"  
               quarters = <- User inputs a number
rabbit ask "How many dimes do you have?" 
               dimes = <- User inputs a number
rabbit ask "How many nickles do you have?" 
               nickles = <- User inputs a number
rabbit ask "How many pennies do you have?"     
               pennies = <- User inputs a number
Now you can add quarters * 0.25 + dimes * 0.1 ... to get the total and compare it to 1.

The second thing you need to deal with is that of variable types. At present you are asking for a string to be received from the user but you need a number or 4 numbers to be exact. The problem is that you cannot treat a string as a number, in other words you cannot add, and multiply strings. You could, using the Int() method, convert a string to a number but this is fraught with peril! In asking for a string you allow the user to type in whatever they want and if they type in something besides a number and you try to convert it your program will likely crash.
Side Note: A variation of this scenario is how most systems are broken into by hackers.
So the best way to get numbers in Alice is to use the "ask user for a number. The user can still mess up your program but it is not as easy. Some examples are:
  • The user can put in a negative number.
  • The user could put in a "real" or non-integer number.
  • The user might be able to put in an expression like 1.1*-3.
In general it is the programmers job to think of how the user could mess things up and try to exclude or correct them before the program goes south.
Mark


Mark Henwood
mhenwood@ieee.org
   
Reply With Quote
Create a change-counting game
Old
exographicskip
Junior Member
 
Status: Offline
Posts: 3
Join Date: Mar 2014
Default Create a change-counting game - 03-04-2014, 06:10 PM

Hey Mark! Thanks for the in-depth explanation of how I should fix the program. I will give those suggestions a try and report back with my findings
   
Reply With Quote
Solution to creating a change-counting game
Old
exographicskip
Junior Member
 
Status: Offline
Posts: 3
Join Date: Mar 2014
Default Solution to creating a change-counting game - 03-14-2014, 05:46 PM

Sorry for the delay, but if anyone is interested, my professor posted the results and it is more elegant than what I tried to hobble together! I have attached it to this post for posterity.
Attached Files
File Type: a2w Chapter 4 Exercise 4 (solution).a2w (1.04 MB, 9 views)
   
Reply With Quote
Print and share the code?
Old
rockuhard
Junior Member
 
Status: Offline
Posts: 2
Join Date: Feb 2018
Location: Jacksonville NC
Post Print and share the code? - 02-08-2018, 08:39 AM

Quote:
Originally Posted by exographicskip View Post
Sorry for the delay, but if anyone is interested, my professor posted the results and it is more elegant than what I tried to hobble together! I have attached it to this post for posterity.
On my Collages blackboard I can download the files (a2w) but nothing happens other than a blank page on my browser. Can you or someone print the code and make a PDF? I am a little lost and cannot download until I'm able to get home. But It would help out a ton now
   
Reply With Quote
bump
Old
rockuhard
Junior Member
 
Status: Offline
Posts: 2
Join Date: Feb 2018
Location: Jacksonville NC
Exclamation bump - 02-08-2018, 05:36 PM

Quote:
Originally Posted by rockuhard View Post
On my Collages blackboard I can download the files (a2w) but nothing happens other than a blank page on my browser. Can you or someone print the code and make a PDF? I am a little lost and cannot download until I'm able to get home. But It would help out a ton now
I too have the same problem. I was able to download this at my a collage computer but neither of my 2 laptops using Chrome and trie IE explorer (Win 7), I have a Windows 10 laptop but same problem, but I will try Edege and see what happens. I may even try a few other browsers.
   
Reply With Quote
Reply

Tags
relational logical

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.