I am trying to write a Python program that charges the parking fee for how many hours you have parked.
Everything works fine until the minutes exceed 300.
I have played with returns and every time I did that after the input I would get successful completion no output.
When I put in 600 minutes (10 hours) I get a fee of 40 dollars when it should be 30 dollars.
Here is my code:
import math
rate1 = 5
rate2 = 4
rate3 = 3
m = int(input('Please enter the number of minutes parked: '))
if m <= 60:
x = m/60
fee = math.ceil(x) * 5
print('Parking fee for',m,'minutes is $',fee)
elif m>60 & m<=300:
x = m/60
fee = math.ceil(x) * rate2
print('Parking fee for',m,'minutes is $',fee)
elif m>300:
x = m/60
fee = math.ceil(x) * rate3
print('Parking fee for',m,'minutes is $',fee)
else:
print('Invalid input')
output:
Please enter the number of minutes parked: 600
Parking fee for 600 minutes is $ 40
Process finished with exit code 0
if m > 60 & m <= 300:
should be:
if m > 60 and m <= 300:
or
if 60 < m <= 300:
& is the bit-wise AND operator, and is the logical AND operator (this is analogous to the difference between & and && in C, PHP, and Javascript).
Related
Python Learner. Working on a recurring monthly deposit, interest problem. Except I am being asked to build in a raise after every 6th month in this hypothetical. I am reaching the goal amount in fewer months than I'm supposed to.
Currently using the % function along with += function
annual_salary = float(input("What is your expected Income? "))
portion_saved = float(input("What percentage of your income you expect to save? "))
total_cost = float(input("what is the cost of your dream home? "))
semi_annual_raise = float(input("Enter your expected raise, as a decimal "))
monthly_salary = float(annual_salary/12)
monthly_savings = monthly_salary * portion_saved
down_payment= total_cost*.25
savings = 0
for i in range(300):
savings = monthly_savings*(((1+.04/12)**i) - 1)/(.04/12)
if float(savings) >= down_payment:
break
if i % 6 == 0 :
monthly_salary += monthly_salary * .03
monthly_savings = monthly_salary * portion_saved
Thanks for the advice all. My code is getting clearer and I reached correct outputs! The problem was with how and when I was calculating interest. In the case of a static contribution I successfully used the formula for interest on a recurring deposit, here, the simpler move of calculating interest at each month was needed to work with the flow of the loop.
annual_salary = float(input("What is your expected Income? "))
portion_saved = float(input("What percentage of your income you expect to save? "))
total_cost = float(input("what is the cost of your dream home? "))
semi_annual_raise = float(input("Enter your expected raise, as a decimal "))
monthly_salary = float(annual_salary/12)
monthly_savings = monthly_salary * portion_saved
down_payment = total_cost*.25
savings = 0
month = 1
while savings < down_payment :
print(savings)
savings += monthly_savings
savings = savings * (1+(.04/12))
month += 1
if month % 6 == 0 :
monthly_salary += (monthly_salary * semi_annual_raise)
monthly_savings = (monthly_salary * portion_saved)
print("")
print("it will take " + str(month) + " months to meet your savings goal.")
Does something like this work for you? Typically, we want to use while loops over for loops when we don't know how many iterations the loop will ultimately need.
monthly_savings = 1.1 # saving 10% each month
monthly_salary = 5000
down_payment = 2500
interest = .02
savings = 0
months = 0
while savings < goal:
print(savings)
savings = (monthly_salary * monthly_savings) + (savings * interest)
months += 1
if months % 6 == 0 :
monthly_salary += monthly_salary * .03
print("Took " + str(months) + " to save enough")
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. :)
I want to round off to the nearest half hour meaning that If I arrive at 11:15 and leave 11:50, car will still be charged for one half an hour not two. I have tried for the last couple of hours to fix it but I cant seem to know what to do (I recently started learning programming)
import math
PARKING_COST = 1.75
TAX_RATE = 1.13
startHour = eval(input('Input the hour when you entered the parking lot(in 24h time please, no leading zeroes):'))
startMinute = input('Input the minute when you entered the parking lot: ')
endHour = eval(input('Input the hour when you exited the parking lot(in 24h time please, no leading zeroes): '))
endMinute = input('Input the hour when you exited the parking lot: ')
minutesPassed = (60*endHour + int(endMinute))-(60*startHour + int(startMinute))
k=1
if minutesPassed<=(15*k):
k+=1
halfHoursPassed=math.floor(float(minutesPassed)/30)
else:
halfHoursPassed=math.ceil(float(minutesPassed)/30)
subtotal = halfHoursPassed * 1.75
total = subtotal * 1.13
print('*******')
print('Parkinglot')
print('Your time in was',str(startHour)+':'+ startMinute)
print('Your time out was',str(endHour)+':'+ endMinute)
print('You spent','%.0f' %halfHoursPassed,'half hours at our garages')
print('Your subtotal was $' + '%.2f' %subtotal)
print('Your total was $' + '%.2f' %total)
print('Thank you for your stay')
print('*******')
So with full_halves = int(minutesPassed / 30) you can get the "full" 30 minute periods. Then using modulo operator % you get the remainer: remaining = minutesPassed % 30. Now, if this remainer is greater than 15, you should add another full half, otherwise you use the full_halves as is.
The modulo operator % gives back the remainder after floor division //. That is to say that 7 // 3 == 2, 7 % 3 == 1. Indeed these two are mutually defined such that (x // k) * k + (x % k) == x.
You should consider taking the modulo of your minutesPassed and 30 to see where the partial-half-hour lies, then comparing that with 15 to judge whether or not to round up or down.
halfHoursPassed = minutesPassed // 30
if minutesPassed % 30 >= 15:
halfHoursPassed += 1
You can somewhat simplify this by using the builtin divmod, which gives both // and % at once.
halfHoursPassed, partialHalfHour = divmod(minutesPassed, 30)
if partialHalfHour >= 15:
halfHoursPassed += 1
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
So I am new to python and am writing this for an assignment, For example if I run 101 as the input, it will run correctly and display that a I need "1 $100 Bill and 1 $1 Bill" but if I run that I need $125 back, it will execute as "1 $100 Bill and 1 $20 Bill" but not execute the remaining $5 bill. I also realized that I cannot run anything under 100 either but that I can fix. I am trying to understand the If/else statements
#
# Python ATM machine
#
import math
withdrawl = int(input("how much money do you want to withdrawl (max withdrawl is $200): "))
if (withdrawl > 200):
print("You are trying to withdraw too much money, this machine is kind of sucky, have a great day!!")
else:
print("1 100 Dollar Bill")
remainder1 = withdrawl - 100
if ((remainder1 / 20) >= 1):
print (math.trunc(remainder1/20)," 20 Dollar Bills")
else:
print("0 20 Dollar Bills")
remainder2 = remainder1 - (math.trunc(remainder1/20)*20)
if ((remainder2 / 10) >= 1):
print (math.trunc(remainder2/10)," 10 dollar Bills")
else:
print ("0 10 Dollar Bills")
remainder3 = remainder2 - (math.trunc(remainder2/10)*10)
if ((remainder3 / 5) >= 1):
print (math.trunc(remainder3 / 5)," 5 dollar Bills")
else:
print("0 5 dollar bills")
remainder4 = remainder3 - (math.trunc(remainder3/5)*5)
if ((remainder4 / 1) >= 1):
print (math.trunc(remainder3/1)," 1 dollar Bills")
else:
print("Thank you for using our ATM machine")
print ("Thank you for using our ATM machine")
Try to use while loop, like this:
withdraw = int(input("how much money do you want to withdraw (max withdraw is $200): "))
data = []
if (withdraw > 200):
print("You are trying to withdraw too much money, this machine is kind of sucky, have a great day!!")
else:
rest = withdraw
while rest > 0:
if rest > 100:
data.append(100)
rest = rest - 100
if 100 > rest >= 50:
data.append(50)
rest = rest - 50
if 50 > rest >= 20:
data.append(20)
rest = rest - 20
if 20 > rest >= 10:
data.append(10)
rest = rest - 10
if 10 > rest >= 5:
data.append(5)
rest = rest - 5
if 5 > rest > 0:
data.append(1)
rest = rest - 1
Output for 37:
[20, 10, 5, 1, 1]
import math
withdrawl = int(input("how much money do you want to withdrawl (max withdrawl is $200): "))
if (withdrawl > 200):
print("You are trying to withdraw too much money, this machine is kind of sucky, have a great day!!")
else:
if(withdrawl >= 100):
print("1 100 Dollar Bill")
remainder = withdrawl - 100
else:
remainder = withdrawl
if ((remainder / 20) >= 1):
print (math.trunc(remainder/20)," 20 Dollar Bills")
remainder = remainder - (math.trunc(remainder/20)*20)
else:
print("0 20 Dollar Bills")
if ((remainder / 10) >= 1):
print (math.trunc(remainder/10)," 10 dollar Bills")
remainder = remainder - (math.trunc(remainder/10)*10)
else:
print ("0 10 Dollar Bills")
if ((remainder / 5) >= 1):
print (math.trunc(remainder / 5)," 5 dollar Bills")
remainder = remainder - (math.trunc(remainder/5)*5)
else:
print("0 5 dollar bills")
if (remainder != 0):
print (remainder," 1 dollar Bills")
print ("Thank you for using our ATM machine")
So you've forgotten to update the value of the remainder + you don't have to use a different variable as a remainder just one is enough, this code should work for you.
And as #ArthurTacca said every if/else block happens regardless of what happened in the previous one.