Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming
Receive the tools necessary to be the rock star of your field. Our 12-month program teaches you the evolving world of multi-channel marketing as well as the complex issues and opportunities found in the industry.

ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month!
Download and Activate to enter!

Web development can be a daunting task, even for specialists. There is a lot of information to absorb and a lot of technologies to learn in order to manage a superior website. When trying to learn the ropes, developers need a reliable source to introduce new ideas that can be easily implemented. When working on large projects, even web veterans may run into a technology or an aspect of a technology that they are unfamiliar with.

Learn More!


Download to Enter
| Contest Rules

Tutorials | Forums

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 7th, 2012, 08:59 PM
bananapancakes bananapancakes is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 3 bananapancakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 45 m 4 sec
Reputation Power: 0
Change Making program and the compound boolean

I am taking an intro programming class with python and we had to write two versions of this program, one with nested branches and one simplifying it with compound boolean statements. It is supposed to make change from a dollar bill for items under a dollar, where the prices only occur in 5-cent increments. We are not allowed to use the division / operator. I wrote one with simple boolean, like this:

change= 100-price
if price <25:
print ('invalid price')
elif price >100:
print ('invalid price')
else:

if change >=75:
quarters = 3
elif change >=50:
quarters = 2
elif change >=25:
quarters = 1
else:
quarters = 0

if change-25*quarters >= 20:
dimes= 2
elif change-25*quarters >=10:
dimes = 1
else:
dimes = 0

if change-25*quarters-10*dimes >=5:
nickles = 1
else:
nickles = 0

if change != 5*nickles+10*dimes+25*quarters:
print ('invalid price')
else:
print 'your change is'
print quarters, 'quarters'
print dimes , 'dimes'
print nickles, 'nickles'

-------------------------------

I can't figure out how to make this with compound statements, can anybody see what it is that I am missing, and give me a hint about how to proceed? The only one that i can see is the starting invalid price statements at the top, I could combine those with an or statement...otherwise I feel like compound statements would just be redundant and even more cluttered...what is it that I am not seeing?

Thanks!

Reply With Quote
  #2  
Old February 8th, 2012, 10:16 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Click here for more information.
 
Join Date: Aug 2011
Posts: 1,075 b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 4 h 41 m 27 sec
Reputation Power: 98
Madness

Thank you for posting an effort. Good start. Some people merely post homework questions.

Using python is a good learning tool. Did you test your program or merely write it as conjecture? If I were to run your program it would fail on the first line
NameError: name 'price' is not defined

Next, indentation is an essential part of python syntax. Post python code here using code tags. Put your code into the edit window, highlight it with your mouse, and click the # icon found above the edit window. And if you have some other hardware configuration, enter
[c o d e] at the start of your program, and
[ / c o d e ] at the program end.
Remove the spaces between the square brackets.

Great, now that you've decided to actually try python, and to try harder to post python so that we don't have to guess the nesting of your if statements we'll get on with the show.

According to python documents, the compound statements are if while for try with def class
Code:
compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | funcdef
                   | classdef

The code you show contains if. Therefor, contrary to your claim, you used compound statements.

Code:
'''
    Make change using quarters, dimes, and nickels.
    No division!
    Limits: the amount of change is less than a buck and a multiple of 5 cents.

    invoke the doctests as the shell command

    $ python -m doctest -v module_name.py
'''

def change(amount,value):
    '''
        return a 2-tuple of the resulting amount and
        the number of coins used of value value.

        doctests:

        >>> change(45,25)
        (20, 1)
        >>> change(20,10)
        (0, 2)

    '''
    i = 0
    while value <= amount:
        amount -= value
        i += 1
    return (amount, i, )


A solution without compound statements:
index into a dictionary of precomputed change for keys list(range(100))[0:100:5] .

Last edited by b49P23TIvg : February 8th, 2012 at 10:33 AM.

Reply With Quote
  #3  
Old February 9th, 2012, 11:39 PM
bananapancakes bananapancakes is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 3 bananapancakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 45 m 4 sec
Reputation Power: 0
Thank you for responding despite my failure to use the code tags, I will use them in the future, woops!

My teacher used 'simple boolean' to refer to if, else, or elif statements, and 'compound boolean' to refer to and/or statements--- I never even considered that those might not be standard terms! I don't really know how to do much beyond if/else statements right now, so your example is a bit beyond me. I have saved the code though, to see if can figure some things out from it on my own. (My class moves soooooooooo slowly.)

Thanks again for your help!

Reply With Quote
  #4  
Old February 10th, 2012, 12:15 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Click here for more information.
 
Join Date: Aug 2011
Posts: 1,075 b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 4 h 41 m 27 sec
Reputation Power: 98
This hideous assignment forces you to write duplicate code. Abhor duplication! You're subject to copy and paste errors. Hard to read because you have to figure out that the code does the same thing in several places. Hard to change because you have to make same changes in many places all the while remembering to rename your variables. In this version I've made the duplicate code exact to reduce the number of places to remember to change variable names. This is just awful. No loops! No user defined functions. Stupid.
Code:
import sys

print('Enter item cost [5 to 95 in multiples of 5].')
cost = int(sys.stdin.readline())
change = 100 - cost

if 95 < change:
    raise ValueError('Invalid item cost.  Too small.')
if change < 5:
    raise ValueError('Invalid item cost.  Too large.')

v = 25
coin = 0
if v <= change:
    change -= v
    coin += 1
if v <= change:
    change -= v
    coin += 1
if v <= change:
    change -= v
    coin += 1
quarters = coin

v = 10
coin = 0
if v <= change:
    change -= v
    coin += 1
if v <= change:
    change -= v
    coin += 1
dimes = coin

v = 5
coin = 0
if v <= change:
    change -= v
    coin += 1
nickels = coin

if change:
    raise ValueError('Invalid item cost.  Not a multiple of 5.')

if 100 != 25*quarters + 10*dimes + 5*nickels + cost:
    raise NotImplementedError('Incorrect program')

print('Change for $1.00:')
print('\tquarters: %d'%quarters)
print('\tdimes: %d'%dimes)
print('\tnickels: %d'%nickels)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Change Making program and the compound boolean


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.

© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap