I have my instructions in the top of my code. But for some reason when I input a decimal and then return my error message like it is supposed to then I input a valid amount it prints everything twice, once for the valid input then once for the valid input here is what i am getting(below bottom).

01
"""
02
withdraw_money()
03
a.  prompt user for a money amount in US Dollars (no cents, i.e. no
04
    fractional part allowed)
05
b.  print a preamble which says that user will see the breakdown of the
06
    money in banknotes
07
c.  print maximum number of $100 bills that can be given for the amount
08
    given by user
09
d.  print maximum number of $50 bills that can be given for the amount left
10
    over from previous step
11
e.  print maximum number of $20 bills that can be given for the amount left
12
    over from previous step
13
f.  print maximum number of $10 bills that can be given for the amount left
14
    over from previous step
15
g.  print maximum number of $5 bills that can be given for the amount left
16
    over from previous step
17
h.  print maximum number of $2 bills that can be given for the amount
18
    left over from previous step
19
i.  print maximum number of $1 bills that can be given for the amount left
20
    over from previous step
21
"""
22
import math
23
import string
24

25
def withdraw_money():
26
    withdraw = 0
27
    hundreds = 0
28
    fifty = 0
29
    twenty = 0
30
    ten = 0
31
    five = 0
32
    two = 0
33
    one = 0
34
    withdraw = input("Input the amount you would like to withdraw\n--> ")
35
    while withdraw >= 100:
36
        hundreds += 1
37
        withdraw -= 100
38
    while withdraw >= 50:
39
        fifty += 1
40
        withdraw -= 50
41
    while withdraw >= 20:
42
        twenty += 1
43
        withdraw -= 20
44
    while withdraw >= 10:
45
        ten += 1
46
        withdraw -= 10
47
    while withdraw >= 5:
48
        five += 1
49
        withdraw -= 5
50
    while withdraw >= 2:
51
        two += 1
52
        withdraw -= 2
53
    while withdraw >= 1:
54
        one += 1
55
        withdraw -= 1
56
    if withdraw < 1 and withdraw > 0:
57
        print "invalid input"
58
        print "do not insert a decimal"
59
        withdraw_money()
60
    print "Here is the bill breakdown for the amount input"
61
    print hundreds, "number of 100s"
62
    print fifty, "number of 50s"
63
    print twenty, "number of 20s"
64
    print ten, "number of 10s"
65
    print five, "number of 5s"
66
    print two, "number of 2s"
67
    print one, "number of 1s"

and what it is giving me

01
>>>
02
Input the amount you would like to withdraw
03
--> 200.30
04
invalid input
05
do not insert a decimal
06
Input the amount you would like to withdraw
07
--> 253
08
Here is the bill breakdown for the amount input
09
2 number of 100s
10
1 number of 50s
11
0 number of 20s
12
0 number of 10s
13
0 number of 5s
14
1 number of 2s
15
1 number of 1s
16
Here is the bill breakdown for the amount input
17
2 number of 100s
18
0 number of 50s
19
0 number of 20s
20
0 number of 10s
21
0 number of 5s
22
0 number of 2s
23
0 number of 1s

Recommended Answers

All 4 Replies

What language did you write the program in? You need to post the code you have written -- what you posted is ok, but doesn't help anyone to determine the cause of the problem.

That looks a lot like Python code to me. I think the OP has misposted in the C section.

To compound things, he seems to have a load of line numbers included in the code he posted. I've just cleaned it up.
Here's the OPs original python code:

"""
withdraw_money()
a.  prompt user for a money amount in US Dollars (no cents, i.e. no
    fractional part allowed)
b.  print a preamble which says that user will see the breakdown of the
    money in banknotes
c.  print maximum number of $100 bills that can be given for the amount
    given by user
d.  print maximum number of $50 bills that can be given for the amount left
    over from previous step
e.  print maximum number of $20 bills that can be given for the amount left
    over from previous step
f.  print maximum number of $10 bills that can be given for the amount left
    over from previous step
g.  print maximum number of $5 bills that can be given for the amount left
    over from previous step
h.  print maximum number of $2 bills that can be given for the amount
    left over from previous step
i.  print maximum number of $1 bills that can be given for the amount left
    over from previous step
"""
import math
import string

def withdraw_money():
    withdraw = 0
    hundreds = 0
    fifty = 0
    twenty = 0
    ten = 0
    five = 0
    two = 0
    one = 0
    withdraw = input("Input the amount you would like to withdraw\n--> ")
    while withdraw >= 100:
        hundreds += 1
        withdraw -= 100
    while withdraw >= 50:
        fifty += 1
        withdraw -= 50
    while withdraw >= 20:
        twenty += 1
        withdraw -= 20
    while withdraw >= 10:
        ten += 1
        withdraw -= 10
    while withdraw >= 5:
        five += 1
        withdraw -= 5
    while withdraw >= 2:
        two += 1
        withdraw -= 2
    while withdraw >= 1:
        one += 1
        withdraw -= 1
    if withdraw < 1 and withdraw > 0:
        print "invalid input"
        print "do not insert a decimal"
        withdraw_money()

    print "Here is the bill breakdown for the amount input"
    print hundreds, "number of 100s"
    print fifty, "number of 50s"
    print twenty, "number of 20s"
    print ten, "number of 10s"
    print five, "number of 5s"
    print two, "number of 2s"
    print one, "number of 1s"

@boomtifier :
The reason you are getting your output twice is because you are recursively calling the function.

So what is happening is:
The first time through the function, you enter an invalid decimal value. Because of the remainder at the end of the note counting operation, you are displaying an error message and making a recursive call to the withdraw_money function.

The second time through the function, you enter a valid integer value, or a decimal ending in .00. Your note counting code counts the notes, there is no remainder and it prints the notes that you get. The 2nd function call then returns us, bringing us to the end of the first function call where the results from the first value are being displayed.

So with your existing code, if you entered n invalid values and 1 valid value, you would get n+1 results printed!

Your best bet would be to set up a loop and loop until you got a valid value from the user. The simplest modification to your function would be:

def withdraw_money():
    valid = False   # The user is always guilty until proven innocent! ;)

    while not valid: # loop until we get something valid from the user
        # initialise all counters to 0
        withdraw = 0
        hundreds = 0
        fifty = 0
        twenty = 0
        ten = 0
        five = 0
        two = 0
        one = 0

        # Get user input
        withdraw = input("Input the amount you would like to withdraw\n--> ")

        # Determine the quantities of each note denomination
        while withdraw >= 100:
            hundreds += 1
            withdraw -= 100
        while withdraw >= 50:
            fifty += 1
            withdraw -= 50
        while withdraw >= 20:
            twenty += 1
            withdraw -= 20
        while withdraw >= 10:
            ten += 1
            withdraw -= 10
        while withdraw >= 5:
            five += 1
            withdraw -= 5
        while withdraw >= 2:
            two += 1
            withdraw -= 2
        while withdraw >= 1:
            one += 1
            withdraw -= 1

        # If there is a remainder output an error message
        if withdraw < 1 and withdraw > 0:
            print "invalid input"
            print "do not insert a decimal"
        else: # the user entered a valid value
            valid = True

    # If we get here we know the user entered a valid value,
    # so we can output their results!
    print "Here is the bill breakdown for the amount input"
    print hundreds, "number of 100s"
    print fifty, "number of 50s"
    print twenty, "number of 20s"
    print ten, "number of 10s"
    print five, "number of 5s"
    print two, "number of 2s"
    print one, "number of 1s"

I would prefer to go with more general approach, and definately not use the dangerous input in Python 2:

def withdraw_money():
    denoms = (100, 50, 20, 10, 5, 2, 1)
    while True:
        try:
            withdraw = int(raw_input('amount of money: '))
            break
        except Exception as e:
            print('Incorrect input: %s' % e)


    # If we get here we know the user entered a valid value,
    # so we can output their results!
    print("Here is the bill breakdown for the amount input")
    for d in denoms:
        count = withdraw // d
        print('%i x $%i' % (count, d))
        withdraw -= count * d

withdraw_money()

Sorry for the late reply i was in a coma, Thanks anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.