I have a python problem and I can't seem to get to the finish line with this question for homework. Basically, the problem is:
an employee's wage is $36.25p/h. a normal working week is 37 hours.
if he works more than 37 hours he gets paid 1.5 times the normal rate ($54.375)
if the employee sells 6 cars or more a week he receives a bonus of $200 per car sold.
TASK: Write a program that takes as input the number of hours worked and the total number of cars sold for the week, and outputs the car dealer’s total salary for the week.
Here is the code I have so far:
hours = int(input("How many hours were worked?"))
salary = float(input("The salary"))
cars = int(input("Total number of cars sold for the week?"))
total = hours * float(salary)
if hours > 37 and cars > 5:
print("The salary is:",int(cars) * int(200) / 10 + 1.5 * total )
elif hours <= 37:
print("The salary is:",total)
try this:
BASE_HOURS = 37
WAGE_MULTIPLIER = 1.5
CAR_SALES_BONUS = 200
BASE_WAGE_SALES = 5 # Updated after assignment adjustment in comments.
hours = int(input("How many hours were worked? > "))
wage = float(input("The wage > "))
cars = int(input("Total number of cars sold for the week? > "))
if hours > 37:
total = BASE_HOURS * wage + (hours - BASE_HOURS) * (wage * WAGE_MULTIPLIER)
else:
total = hours * wage
if cars > BASE_WAGE_SALES:
total += CAR_SALES_BONUS * (cars - BASE_WAGE_SALES)
# ...
As you can see I took my freedom to refactor the code quite a bit to improve structure & reduce code smell. Furthermore some variable names like salary where quite unfitting if I've interpreted the sense behind the script correctly, as we're interested in the hourly wage of the person, not his salary.
Related
Write a program to calculate how many months it will take you to save up enough money for a down
payment. You will want your main variables to be floats, so you should cast user inputs to floats.
Your program should ask the user to enter the following variables:
The starting annual salary (annual_salary)
The portion of salary to be saved (portion_saved)
The cost of your dream home (total_cost)
Call the cost of your dream home total_cost.
Call the portion of the cost needed for a down payment portion_down_payment. For
simplicity, assume that portion_down_payment = 0.25 (25%).
Call the amount that you have saved thus far current_savings. You start with a current
savings of $0.
Assume that you invest your current savings wisely, with an annual return of r (in other words,
at the end of each month, you receive an additional current_savings*r/12 funds to put into
your savings – the 12 is because r is an annual rate). Assume that your investments earn a
return of r = 0.04 (4%).
Assume your annual salary is annual_salary.
Assume you are going to dedicate a certain amount of your salary each month to saving for
the down payment. Call that portion_saved. This variable should be in decimal form (i.e. 0.1
for 10%).
At the end of each month, your savings will be increased by the return on your investment,
plus a percentage of your monthly salary (annual salary / 12).
Expected case
Enter your annual salary: 120000
Enter the percent of your salary to save, as a decimal: .10
Enter the cost of your dream home: 1000000
Number of months: 183
My code text output
Number of months = 178
r = 0.04
current_savings = 0
potion_down_payment = 0.25
annual_salary = input('Enter your annual salary: ')
print(int(annual_salary))
monthly_salary = (int(annual_salary) / 12)
potion_saved = input('Enter the percent of your salary to save, as a decimal: ')
print(float(potion_saved))
cost_dream_home = input('Enter the cost of your dream home : ')
print(int(cost_dream_home))
invest_return = ((float(r) * float(annual_salary)) / 12)
amount_to_save = (float(potion_down_payment) * int(cost_dream_home))
potion_saved_f = (float(monthly_salary) * float(potion_saved))
total_saved = (float(invest_return) + potion_saved_f)
number_of_months = (float(amount_to_save) // float(total_saved))
print(int(number_of_months))
I did this quickly, but you need a while loop, like:
months = 0
target = 250000
monthly_save = 1000.0
saved = 0.0
rate = .04
subtotal = 0.0
while subtotal < target:
saved += monthly_save
subtotal = (subtotal + monthly_save) * (1+(rate/12))
months += 1
print(months), print(int(subtotal))
OK, I have more time now. I think that you are just lucky that your code came out so close to the expected answer because your computations are off base. Unless you know of a better formula, you really need a loop in order to compute compound interest month by month. I have analyzed your method below.
invest_return = ((float(r) * float(annual_salary)) / 12) #.04*120000/12=400
amount_to_save = (float(potion_down_payment) * int(cost_dream_home))#.25*1000000=250000
potion_saved_f = (float(monthly_salary) * float(potion_saved))#10000*.1=1000
total_saved = (float(invest_return) + potion_saved_f) #400+1000
number_of_months = (float(amount_to_save) // float(total_saved)) #250000//1400 = 178
print(int(number_of_months))
I'm working on a project in Python for a salesman. The project takes the pay from hours worked for the week and the percentage from commission of sales per week and adds them together to give the total weekly pay of the salesman. The problem I'm running into is dividing the sales amount with the percent they receive from each sale. The error is "cannot assign to operator"
#Prompt user for the number of hours worked that week
hours_worked = int(input("How many hours did you work this week? \n"))
#Define hourly wage
per_hour = 30
#Multiply the hourly wage and hours worked that week
total_hours_pay = hours_worked * per_hour
#Prompt user for the number of sales made that week
sales_made = int(input("How many sales did you make this week? \n"))
#Amount of each sale
sales = 250
#Determine commission of each sale
25 / sales = commission
#Multiply the commission times the amount of sales made to retrieve amount of total commission pay
total_sales_pay = sales_made * commission
#Add total commission pay from sales with the total hours pay to get the weekly total
total_week_pay = total_sales_pay + total_hours_pay
print(total_week_pay)
You wrote:
25 / sales = commission
But when assigning something to a variable, you need to put the variable at the start of the statement.
Try
commission = 25 / sales
25 / sales = commission this line throws the error. I think it should be comission = 25 / sales. You try to assign the value of comission to the result of 25 / sales which does not work.
I am trying to translate the below statement into Python but struggle with a reoccuring error.
hours = input (prompt1)
^
IndentationError: expected an indented block
A worker gets paid an hourly wage, when working up to 40 hours per week. When the time at work goes above 40 hours in a week, the worker is being paid overtime rate, which is 1.5 times the hourly wage. Given the hours worked per week, and the hourly wage, compute the weekly salary of the worker.
prompt1 = 'How many hours did you work?\n'
try:
hours = input (prompt1)
prompt2 = 'What is your hourly rate?\n'
rate = input (prompt2)
hours = float(hours)
rate = float(rate)
print float(hours) * float(rate*1.5)
except:
print('Error, Please enter a number')
I really appreciate your help.
In Python there are no {} or begin +end it works using identation. You code should look like:
prompt1 = 'How many hours did you work?\n'
try:
hours = input (prompt1)
prompt2 = 'What is your hourly rate?\n'
rate = input (prompt2)
hours = float(hours)
rate = float(rate)
if(hours > 40):
rate = rate * 1.5
print float(hours) * float(rate*1.5)
except:
print('Error, Please enter a number')
Here is my assignment:
Write a program to calculate the credit card balance after one year if
a person only pays the minimum monthly payment required by the credit
card company each month.
The following variables contain values as described below:
balance - the outstanding balance on the credit card
annualInterestRate - annual interest rate as a decimal
monthlyPaymentRate - minimum monthly payment rate as a decimal
For each month, calculate statements on the monthly payment and
remaining balance, and print to screen something of the format:
Month: 1
Minimum monthly payment: 96.0
Remaining balance: 4784.0
Finally, print out the total amount paid that year and the remaining
balance at the end of the year in the format:
It should not specify the values for the variables balance,
annualInterestRate, or monthlyPaymentRate - our test code will define
those values before testing your submission.
And here is the code I wrote:
def minpayment(balance, annualInterestRate, monthlyPaymentRate):
totalPaid = 0
month = 1
while month <= 12:
minPayment = monthlyPaymentRate * balance
balance -= minPayment
balance += (annualInterestRate/12.0)*balance
print 'Month:',month
print 'Minimum monthly payment:',round(minPayment,2)
print 'Remaining balance:',round(balance,2)
totalPaid += minPayment
month += 1
print 'Total paid:', round(totalPaid,2)
print 'Remaining balance:', round(balance,2)
Now, my question is, now that I've created the function, how do I call it?
You can call it like:
minpayment(10000, 0.1, 0.3)
or in general:
# You can modify the values below
balance = 10000
annualInterestRate = 0.1
monthlyPaymentRate = 0.3
minpayment(balance, annualInterestRate, monthlyPaymentRate)
I've got an assignment for my MITx CS class and I am stuck on the following problem:
The following variables contain values as described below:
balance - the outstanding balance on the credit card
annualInterestRate - annual interest rate as a decimal
To recap the problem: we are searching for the smallest monthly
payment such that we can pay off the entire balance within a year.
What is a reasonable lower bound for this payment value? $0 is the
obvious anwer, but you can do better than that. If there was no
interest, the debt can be paid off by monthly payments of one-twelfth
of the original balance, so we must pay at least this much every
month. One-twelfth of the original balance is a good lower bound.
What is a good upper bound? Imagine that instead of paying monthly, we
paid off the entire balance at the end of the year. What we ultimately
pay must be greater than what we would've paid in monthly
installments, because the interest was compounded on the balance we
didn't pay off each month. So a good upper bound for the monthly
payment would be one-twelfth of the balance, after having its interest
compounded monthly for an entire year.
In short:
Monthly interest rate = (Annual interest rate) / 12.0 Monthly payment
lower bound = Balance / 12 Monthly payment upper bound = (Balance x (1
+ Monthly interest rate)12) / 12.0
Write a program that uses these bounds and bisection search (for more
info check out the Wikipedia page on bisection search) to find the
smallest monthly payment to the cent (no more multiples of $10) such
that we can pay off the debt within a year. Try it out with large
inputs, and notice how fast it is (try the same large inputs in your
solution to Problem 2 to compare!). Produce the same return value as
you did in Problem 2.
Now, this is what I've been able to come up with but it actually generates a wrong output. I have no idea what's going on wrong with this code:
#------------Defined variables---------------#
balance = 999999
annualInterestRate = 0.18
#------------Defined variables---------------#
monthlyInterestRate = annualInterestRate / 12.0
monthlyPaymentLower = balance / 12
monthlyPaymentUpper = (balance * (1 + monthlyInterestRate)**12) / 12.0
month = 1
total = 0
while (total < balance) and month < 13:
pay = (monthlyPaymentLower + monthlyPaymentUpper) / 2
total += pay
if total < balance:
monthlyPaymentLower = pay
elif total > balance:
monthlyPaymentHigher = pay
month += 1
if month == 13:
total = 0
month = 1
print 'Lowest Payment: ' + str(round(pay, 2))
Help?
Like always, not looking for a complete solution or the source code, just drop some hints on where I've gone wrong. (I always get negative votes. :/ )
In place
total += pay
you have to count balance for 12 mount like in problem 2
for month in range(12):
balance = balance - pay
balance = balance + monthlyInterest
than you have to compare balance with 0 and change monthlyPaymentLower or monthlyPaymentUpper
You have to compare monthlyPaymentLower to monthlyPaymentUpper to see if you can finish
if monthlyPaymentUpper - monthlyPaymentLower < 0.001 :
print "now I can finish searching"
Of course in this situation you will have to change more things in your code :)