1

I am converting my greedy.py function and since python does not have datatypes, i'm getting really confused as to why my code does not convert to an int w/out a .0 ending.

Code:

import cs50

class Balance:
    def __init__(self, amt, remaining):
        self.amt = amt
        self.remaining = remaining
def amt_coins(moneytype, total):
    amt = (total - (total % moneytype)) / moneytype
    remaining = (total - (amt * moneytype))
    return Balance(amt, remaining)
while True:
    print("O hai! How much money is owed?")
    moneyinput = cs50.get_float();
    if (moneyinput > 0):
        break
cents = int(round(moneyinput * 100))
q = amt_coins(25, cents);
d = amt_coins(10, q.remaining)
n = amt_coins(5, d.remaining)
p = amt_coins(1, n.remaining)
print("{}".format(q.amt + d.amt + n.amt + p.amt))

Example output:

O hai! How much money is owed?
1.24
10.0
2

First a couple of stylistic problems I noticed. Per PEP8, which is the style guide for Python, and more specifically the BlankLines part, you should:

Surround top-level function and class definitions with two blank lines.

Method definitions inside a class are surrounded by a single blank line.

Additionally, Python does have data types, you just don't have to explicitly set them. You can find the type of a variable/expression by using the type() function like so:

print("type of q.amt + d.amt + n.amt + p.amt: {}".format(type(q.amt + d.amt + n.amt + p.amt)))

which prints

type of q.amt + d.amt + n.amt + p.amt: <class 'float'>

So the python interpreter correctly adds the decimal places, because this addition has a result a float. Now let's see why this might happen.

In your amt_coins() function, you have:

amt = (total - (total % moneytype)) / moneytype

if you remember from the lectures, prof. Malan says that unlike C, in Python, the / denotes not an integer, but a floating point division. So amt is a float.

In order to perform an integer division instead of the default floating point one, use the // operator instead of the / operator.

Also round(), according to the documentation, returns an integer in case there is no second argument passed for the number of decimal places, so you can remove the int() around round().

If ndigits is omitted or is None, it returns the nearest integer to its input.

Here's your program after the corrections.

import cs50


class Balance:
    def __init__(self, amt, remaining):
        self.amt = amt
        self.remaining = remaining


def amt_coins(moneytype, total):
    amt = (total - (total % moneytype)) // moneytype
    remaining = (total - (amt * moneytype))
    return Balance(amt, remaining)


while True:
    print("O hai! How much money is owed?")
    moneyinput = cs50.get_float()
    if (moneyinput > 0):
        break

cents = round(moneyinput * 100)
q = amt_coins(25, cents)
d = amt_coins(10, q.remaining)
n = amt_coins(5, d.remaining)
p = amt_coins(1, n.remaining)

print("{}".format(q.amt + d.amt + n.amt + p.amt))

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .