Coding Exercise: Timbits-Fix the bugs and pass all of the tests - python

I've been looking at this program for a while now and I can't seem to find what is wrong with the code. I've checked the numbers and they are fine. I almost think its a quotes or parentheses. I would appreciate the help. Here's the code:
# step 1: get the input
timbitsLeft = int(input())
# step 2: initialize the total cost
totalCost = 0
# step 3: buy as many large boxes as you can
bigBoxes = int(timbitsLeft / 40)
totalCost = totalCost + bigBoxes * 6.19 # update the total price
timbitsLeft = timbitsLeft - 40 * bigBoxes # calculate timbits still needed
# step 4, can we buy a medium box?
if timbitsLeft >= 20:
totalCost = totalCost + 3.39
timbitsLeft = timbitsLeft - 20
if timbitsLeft > 10: # step 5, can we buy a small box?
totalCost = totalCost + 1.99
timbitsLeft = timbitsLeft - 20
# step 6
totalCost = totalCost + timbitsLeft * 20
print(totalCost)
This is the error I get:
Did not pass tests. Please check details below and try again.
Results for test case 1 out of 11
Input:
10
Program executed without crashing.
Program output:
200.0
Expected this correct output:
1.99
Result of grading: Output line 1, value 200.0, did not match expected value 1.99

You are getting an output of 200 because you do not have enough money to buy a large box or medium box. You then check to see if you can buy a small box, but you only have 10 timbits, so the if statement, if timbitsLeft > 10: # step 5, can we buy a small box?, is not true so you cannot buy a small box either. Then you do the calculation totalCost = totalCost + timbitsLeft * 20 which gives you a value of 200.

Well, looks like your mistake is
if timbitsLeft > 10:
Your input is 10 so you have 10 timbits left,
but you need more than 10 to go further into the if statement,
therefore its not doing anything except:
totalCost = totalCost + timbitsLeft * 20
and thats basically
totalCost = 0 + 10 * 20
and that is indeed 200
you might need
if timbitsLeft >= 10:

You didn't read all of the information given to you.
The numbers are wrong for the small box and the price of 1 box.
if timbitsLeft > 10: # step 5, can we buy a small box?
totalCost = totalCost + 1.99
timbitsLeft = timbitsLeft - 20 (<---- the problem. it should be: - 10)
-10 is the amount of small boxes.
totalCost = totalCost + timbitsLeft * 20 (<---- the problem. it should be: *.2)
print(totalCost)
.2 is the price of a single box.

here is the solution to the cscircles "Coding Exercise: Timbits"
timbitsLeft = int(input()) # step 1: get the input
totalCost = 0 # step 2: initialize the total cost
# step 3: buy as many large boxes as you can
if timbitsLeft >=40:
bigBoxes = int(timbitsLeft / 40)
totalCost = totalCost + bigBoxes * 6.19 # update the total price
timbitsLeft = timbitsLeft - 40 * bigBoxes # calculate timbits still needed
if timbitsLeft >= 20: # step 4, can we buy a medium box?
mediumBoxes = int(timbitsLeft / 20)
totalCost = totalCost + mediumBoxes * 3.39
timbitsLeft = timbitsLeft - 20 * mediumBoxes
if timbitsLeft >= 10: # step 5, can we buy a small box?
smallBoxes = int(timbitsLeft / 10)
totalCost = totalCost + smallBoxes*1.99
timbitsLeft = timbitsLeft - smallBoxes*10
totalCost = totalCost + timbitsLeft * 0.2 # step 6
print(totalCost) # step 7

Related

Why am I printing different values?

What's different between lines 16 and 17?
#user input
annual_salary = float(input("Enter your annual salary: "))
portion_saved = float(input("Enter the percent of your salary to save, as a decimal: "))
total_cost = float(input("Enter the cost of your dream home: "))
#static vars
portion_down_payment = total_cost*.25
monthly_salary = annual_salary/12
r = .04 #annual rate of return
months_to_save = 0
current_savings = 0
investment_return = current_savings * r / 12
while current_savings < portion_down_payment:
#current_savings += (current_savings * r / 12) + (portion_saved * monthly_salary) # Line 16
current_savings += (investment_return) + (portion_saved*monthly_salary) # Line 17
months_to_save += 1
print("Number of months: ", months_to_save)
I tried running it through pythontutor and the variation happens on step 15 of execution, but I can't quite figure out what's different.
When you use current_savings += investment_return, it adds the same amount of interest to current_savings each time through the loop. That interest is equal to current_savings * r / 12 calculated before you started the loop.
But when you use current_savings += (current_savings * r / 12), you recalculate the interest each time through the loop. So the interest is calculated based on the current value of current_savings, which gets bigger each time the loop runs.
In other words, the first one calculates simple interest, and the second one calculates compound interest.

How do I fix this hourly rate calculator?

Keep getting bad input errors on this python code. Can someone walk me through what I'm doing wrong? Thanks. The task is that the code works out time-and-a-half for the hourly rate for all hours worked above 40 hours. Using 45 hours and a rate of 10.50 per hour to test the program, the pay should then be 498.75. I keep getting 708.75...
hrs = input("Enter Hours:")
h = float(hrs)
rate = input("Enter Rate:")
r = float(rate)
double_r = r * 1.5
total = 0.0
if h <= 40.00:
total = h * r
elif h > 40.00:
total = h * double_r
print(total)
hrs = float(input("Enter Hours: "))
rate = float(input("Enter Rate: "))
double_rate = rate * 1.5
total = 0.0
if hrs <= 40.00:
total = hrs * rate
elif hrs > 40.00:
total = ((hrs - 40 ) * double_rate) + (40 * rate)
print(total)
Your problem isn't a coding problem but a math problem: You're multiplying every hour with the double_r rate (45 * 10.5 * 1.5 = 708.75). If you only want the hours above 40 hours to be multiplied with the higher rate then you have to multiply them extra (40 * r for the normal rate and (h-40) * double_r for the rest with the better rate. Your code should look like this:
if h <= 40.00:
total = h * r
elif h > 40.00:
total = 40 * r + (h - 40) * double_r
Is this bad input error or logical error ?
I don't have solution for first one, but I surely have it for the second part.
According to your code, If hours are <=40 , you are multiplying the hour with the rate.
but if it greater than 40, you are multiply the hour with the rate with 1.5 .
Here the logic is going wrong.
You just need to add the extra 1.5 for those hours which are greater than 40.
For that, you would have to modify your total statement.
Something like this :
total = ((h - 40 ) * double_r) + (40 * r)
So for 45 hours with 10.5 rate ,
it would be 40 * 10.5 = 420
and 510.51.5 = 78.75
thus resulting in 498.75
If this helps, please upvote. :)

why does my iteration count start at 2 instead of one?

I have to write a program showing the total number of payments and total amount paid for a mortgage. The problem assumes an extra $1000 a month for the first 12 months. The answer $929,965.62 over 342 months. The output I get is $929,965.62 over 343 months. The problem is my code starts counting at 2 but the first amount is correct.
principal = 500000.0
rate = 0.05
payment = 2684.11
total_paid = 0.0
extra_payment = 1000
payment_number = 1
while principal > 0 and payment_number <=12:
principal = principal * (1+rate/12) - (payment + extra_payment)
total_paid = total_paid + (payment + extra_payment)
payment_number += 1
print(payment_number, round(total_paid, 2))
else:
while principal > 0:
principal = principal * (1+rate/12) - payment
total_paid = total_paid + payment
payment_number += 1
print(payment_number, round(total_paid, 2))
I don't understand why the above code starts at 2 and the code below starts counting at 1.
height = 100
bounce = 1
while bounce <= 10:
height = height * (3/5)
print(bounce, round(height, 4))
bounce += 1
First example you print after you increment payment_number; second sample it's reversed. Change
payment_number += 1
print(payment_number, round(total_paid, 2))
to
print(payment_number, round(total_paid, 2))
payment_number += 1

Python cost check (Closed)

i am making a function that inputs weight and returns cost. My code is correct and I did my math but the cost being returned isn't making any sense
def cost_check(weight):
if weight <= 2:
cost = (weight * 1.50) + 20.00
elif weight > 2:
cost = (weight * 3.00) + 20.00
elif weight > 6:
cost = (weight * 4.00) + 20.00
else:
cost = (weight * 4.75) + 20.00
return cost
print(cost_check(8.4))
when I call the function it is supposed to return 53.60. BUT instead it returns 45.2 for some reason, it is really frustrating
This can not work because these statements will never be reached:
elif weight > 6:
cost = (weight * 4.00) + 20.00
else:
cost = (weight * 4.75) + 20.00
Suppose you have weight = 3. Then it will match the statement
elif weight > 2:
cost = (weight * 3.00) + 20.00
Because the next statements are elif and else they will be skipped
ok so i changed it to:
def cost_check(weight):
cost = 0
if weight <= 2:
cost = (weight * 1.50) + 20.00
elif weight > 2 and weight <= 6:
cost = (weight * 3.00) + 20.00
elif weight > 6 and weight <= 10:
cost = (weight * 4.00) + 20.00
else:
cost = (weight * 4.75) + 20.00
return(cost)
and it worked as it was supposed to, thanks anyways for the help guys!

Python bisection search not running and no error message. MIT edX

The problem is from the MIT edX Python Course 6.00.1 Problem Set 1 Part C. Here are the problems. Scroll to part C. I'm aware that there are countless questions asked about the edX course but none of them have really helped me. Whenever I run my bisection search code, nothing happens. No error message, nothing. Could someone help me find the issue in my code? Sorry if code is horribly inefficient, very new to python and programming.
#Python script for finding optimal saving rate of monthly salary in order to purchase 1M house in 36 months
salary = float(input("Enter salary: "))
total_cost = 1000000
salary_raise = 0.07 #semiannual raise rate
down = 0.25 * total_cost #downpayment
steps = 0
r = 0.04 #annual investments returns
low = 0 #low parameter for bisection search
high = 10000 #high parameter
current_savings = 0
while (current_savings <= (down - 100)) or (current_savings >= (down + 100)):
current_savings = 0
monthly_salary = salary/12
guess_raw = (high + low)/2
guess = guess_raw/10000.0
months = 0
steps += 1
while months < 36: #Finds end amount of money after 36 months based on guess
months += 1
multiple = months%6
monthly_savings = monthly_salary * guess
current_savings = current_savings + monthly_savings + current_savings*r/12
if multiple == 0:
monthly_salary += salary_raise * monthly_salary
if (current_savings >= (down - 100)) and (current_savings <= (down + 100)): #If the guess is close enough, print the rate and number of steps taken
print ("Best savings rate: ",guess)
print ("Steps in bisection search: ",steps)
break
elif current_savings < (down - 100): #If the guess is low, set the low bound to the guess
if guess == 9999:
print ("It is not possible to pay the down payment in three years.")
break
else:
low = guess
elif current_savings > (down + 100): #If the guess is high, set the high bound to the guess
high = guess

Categories