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. :)
Related
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.
I'm converting from all units of time to seconds and for some reason with the smaller units of time (picoseconds and femtoseconds), i'm getting (femtoseconds): 0.00000000000000100000000000000007770539987666107923830718560119501514549256171449087560176849365234375 instead of 0.000000000000001. Does anyone know why?
days = int(input("Enter the amount of days: ")) * 24 * 60 * 60
hours = int(input("Enter the amount of hours: ")) * 60 * 60
minutes = int(input("Enter the amount of minutes: ")) * 60
ms = int(input("Enter the amount of milliseconds: ")) * (10 ** -3)
mcs = int(input("Enter the amount of microseconds: ")) * (10 ** -6)
ns = int(input("Enter the amount of nanoseconds: ")) * (10 ** -9)
ps = int(input("Enter the amount of picoseconds: ")) * (10 ** -12)
fs = int(input("Enter the amount of femtoseconds: ")) * (10 ** -15)
s = days + hours + minutes + ms + mcs + ns + ps + fs
print("The amount of seconds is:", "{0:.50}".format(s))
Floating point numbers cannot be accurately represented in any programming language, simply because there is an infinite number of them. However, what might help you is Decimal: Clarification on the Decimal type in Python
Documentation: https://docs.python.org/3.8/library/decimal.html
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).
Hi i am new to programming and just started learning python i wrote the below code /program to prompt hours and rate per hour using raw_input to compute gross pay. i tried to initiate time-and-a-half for the hourly rate for all hours worked above 40 hours. the logic to do the computation of time-and-a-half in a function called computepay() and use the function to do the computation. The function should return a value. i used 45 hours and a rate of 10.50 per hour to test the program (the pay should return 498.75). i tried using raw_input to read a string and float() to convert the string to a number. i Don't name my variable sum or use the sum() function. i am able to print the output but i need to input the data when prompted unlike inputing the value in "line16"
def computePay (hours,rate):
if hours > 40:
overtimerate = 1.5 * rate
overtime = (hours-40) * overtimerate
overtimepay = overtime + (40 * rate)
return overtimepay;
else:
normalpay = hours * rate
return normalpay;
hours = raw_input('Enter hours: ')
hrs = int(hours)
rate = raw_input('Enter rate: ')
r = float(rate)
p = computePay(45,10.50)
print p
Assumptions:
I had to make the following assumptions in understanding your question:
Looking at your print statement, You are using python2.x
You want to computePay using the user inputted hours and rate.
Problem:
In the following line you are using constant hrs and r values instead of using the user inputed values
p = computePay(45,10.50)
Solution:
If you want to use user inputted values to compute the pay, you need to call the function as follows:
p = computePay(hrs, r)
With this line you are essentially asking python to computePay using the values stored in the variables hrs and r.
Final Code:
Therefore your final code should like this:
def computePay (hours,rate):
if hours > 40:
overtimerate = 1.5 * rate
overtime = (hours-40) * overtimerate
overtimepay = overtime + (40 * rate)
return overtimepay;
else:
normalpay = hours * rate
return normalpay;
hours = raw_input('Enter hours: ')
hrs = int(hours)
rate = raw_input('Enter rate: ')
r = float(rate)
p = computePay(hrs,r)
print p
Sample Output:
Enter hours: 55
Enter rate: 20
1250.0
If you mean by that you want the arguments to equal the inputs rather than the ones inputted manually, you should do instead:
p = computePay(hrs, r)
Example output:
Enter hours: 45
Enter rate: 10.5
498.75
So I need help with programming.
My assignment is this:
Write a program to prompt the user for hours and rate per hour using raw_input to compute gross pay. Award time-and-a-half for the hourly rate for all hours worked above 40 hours. Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75). You should use raw_input to read a string and float() to convert the string to a number. Do not worry about error checking the user input - assume the user types numbers properly.
I did this:
inp = raw_input ('Enter Hours: ')
hours = float(inp)
inp = raw_input ('Enter Rate: ')
rate = float(inp)
print rate, hours
if hours <= 40 :
pay = hours * rate
else :
pay = rate * 40 + (rate * 1.5 * ( hours - 40 ))
print pay
And it seemed to be okay but when I click on check the code, I enter hours 45, and then rate I tried entering 10.50, 10.5 but every time I get this:
10.5 45.0 ← Mismatch
498.75
The answer 498.75 is correct but I keep getting mismatch there so I cannot finish my assignment. Anyone knows what am i doing wrong?
To print float with your format you should use format string (examples).
So you should change line:
print rate, hours
to:
print("Rate = %.2f, Hours = %.0f" % (rate, hours))
# ^ ^
# | Remove all chars after point (may be you need to change that
# according your task)
# Use to chars after comma (no zeros removing)
By using a function you can do it
def computepay(h,r):
if (h>40) :
pay = (40*r)+(h-40)*1.5*r
else:
pay = (h*r)
return pay
try:
inp = raw_input("Please enter hours: ")
hours=float(inp)
inp = raw_input("Please enter rate: ")
rate= float(inp)
except:
print "Please enter a number as input"
quit()
print computepay(hours,rate)
It seems that print rate, hours produces output which the checking program does not expect, and cannot cope with. Simply comment out that line.
hrs = raw_input("Enter Hours:")
h = float(hrs)
rate = raw_input("Enter rate:")
r = float(rate)
pay = h * r
print pay
This would be the answer to your question #user3578390
4.6 Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Pay should be the normal rate for hours up to 40 and time-and-a-half for the hourly rate for all hours worked above 40 hours. Put the logic to do the computation of pay in a function called computepay() and use the function to do the computation. The function should return a value. Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75). You should use input to read a string and float() to convert the string to a number. Do not worry about error checking the user input unless you want to - you can assume the user types numbers properly. Do not name your variable sum or use the sum() function.
def computepay(h,r):
if h <= 40:
return h * r
elif h > 40:
return (40 * r + ((h - 40) * 1.5 * r))
hrs = float(input("Enter Hours:"))
rate = float(input("Enter Rate:"))
p = computepay(hrs, rate)
print("Pay",p)
I did this:
hrs = raw_input("Enter Hours:")
h = float(hrs)
rate = raw_input("Enter rate:")
r = float(rate)
pay = h * r
if h <=40:
pay = h * r
else:
pay = r * 40 + (r * 1.5 * ( h - 40 ))
print pay