0

I have asked to reduce the code to removing duplication :

june_hours = 243
june_cost = june_hours * 0.65
print("In June we spent: " + str(june_cost))

july_hours = 325
july_cost = july_hours * 0.65
print("In July we spent: " + str(july_cost))

august_hours = 298
august_cost = august_hours * 0.65
print("In August we spent: " + str(august_cost))

I tried to :

def print_monthly_expense(month, hours):
    time = hours * 0.65
    print("In " + month + " We spent : " + str(time))

print_monthly_expense("June",243)
print_monthly_expense("July",325)
print_monthly_expense("August",298)

And the result is :

In June We spent : 157.95000000000002
In July We spent : 211.25
In August We spent : 193.70000000000002

And the system replied :

Not quite. Let your function do most of the work and just pass the name of the month and the associated hours as parameters.

Remember for June, July and August the hours are 243, 325 and 298 respectively.

Please help me, whats wrong with the code?

thanks in advance

  • 1
    It appears that the system is expecting those exact numbers. Call `str(int(time))` to round them properly. – clubby789 Jan 21 '20 at 12:46
  • Please explain your expected outputs, without simply quoting "the reply of the system". It is not crystal clear. – Laurent H. Jan 21 '20 at 12:47
  • What is the "system" that replied? Your function looks okay to me and it prints the same values as the original code. For the weird floating points see https://stackoverflow.com/questions/588004/is-floating-point-math-broken – Ocaso Protal Jan 21 '20 at 12:49
  • I don't know the expected output, but with this output the system is not satisfied. – Monirul Islam Jan 21 '20 at 13:21
  • Please see the original codes, which needs to be shorten as possible. Anyone have better suggestions than what I did, please share with me. – Monirul Islam Jan 21 '20 at 13:50

1 Answers1

0

Monirul, looks like we're both working on the same thing. I had the same error you had when I first ran my code.

I had this code at first:

def print_monthly_expense(month, hours):
    cost = (hours) * 0.67
    print("In "  + month + " we spent: " + str(cost))

print_monthly_expense("June",243)
print_monthly_expense("July",325)
print_monthly_expense("August",298)

The first few times I got ran it, I got the same error you had and I couldn't figure it out. Then I realized the 0.67 was wrong, I should have used 0.65. When I fixed the error, the code ran correctly and I got this message.

Here is your output:
In June we spent: 157.95000000000002
In July we spent: 211.25
In August we spent: 193.70000000000002

Nice work! You're getting acquainted with some interesting
coding practices to reduce code duplication.

Did you figure out what was wrong? Maybe it's the text formatting? You have upper case "We" and a space before the colon at the end "spent :"? Just guessing, I don't see anything else.

daturn
  • 1