If Then Else Statements - python

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')

Related

Why is my program outputting 178 instead of 183?"

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))

python program for save days and hours in a list or dictionary , and when i updated my days and hours they will be added in total

i want to track and save my working days and hours.
so in this program, the days and hours won't be saved.its crap..
day = int(input('number of day : '))
total_day = 0 + day
update_day = total_day + day
hours = int(input('enter your today work hours : '))
total_hours = 0 + hours
update_hours = total_hours + hours
print(f"# your total day is = {update_day} and \n# total hours is = {update_hours}")
i want to save the days and hours in total days & hours maybe in a dictionary, and when i updated my days and hours they will be added in total.

Calculation of wage based on inputs in python

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.

Working on a Python project for a salesman. Getting "cannot assign to operator" error

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.

Python loan table trouble

I'm currently trying to figure out how to iterate over over columns and rows at the same time [if possible]. Or wondering what the best way to approach this is. The biggest problem that I'm having is creating for loops with floats in simple ways. Our professor specifically asks for us to do that and then when we have to fill out the rest of the information basically accessing those loops. He doesn't want us to be importing different libraries and says there are simple ways for us to do this but doesn't really gives us guidance :/ Our table is supposed to be formatted to look like this:
this is what i have in total already..
Supporting Code:
def main():
print("This program is used to analyze commercial real estate loans.")
#function variables
purchase_price = input("Please input original purchase price: ")
down_payment = input("Please input total down payment: ")
APR = float(input("Please input APR: "))
periods_year = input("Please input the number of payments per year: ")
term = float(input("Please input loan length in years: "))
first_payment = input("Please input date of first payment in form MM/DD/YYYY: ")
#seperating str
dates = first_payment.split('/')
day = int(dates[0])
month = int(dates[1])
year = int(dates[2])
#part 1 calculations with main variables
total_payments = float(term) * float(down_payment)
loan_amount = float(purchase_price) - float(down_payment)
total_amount = loanCalc(APR, periods_year, loan_amount, total_payments)
end_date = str(day) + "/" + str(month) + "/" + str(year + int(term))
total_interest = r*total_payments
#print("End day of loan : %s" %end_date) [do not need to print this right now]
#loancalc function
def loanCalc(APR, period_year, loan_amount, total_payments):
#interest charged per period
r = (APR)/(period_year)
#amount paid in each period
P = (r * loan_amount)/(1- (1+r) ** (-(total_payments)))
#amount paid over life of loan
final_value = (P * total_payments)
#remember to return not print
return final_value
Code that needs a loop
#loan payment table part 3/table will display
def pmtOptions(APR,period_year, term,loan_amount):
#headers, each column will be 10
print("{0:^80}".format("Alternative Loan Payment Table"))
print("{0:^80}".format("=============================="))
print("{0:^80}".format("Interest Rates"))
#interest rate loop
##for APR in range(3,6):
## if APR == 3:
## print("3.25",)
## APR = 3.25
## while APR <= 5.75:
## APR +=.5
## print("{0:>3}".format(""),"{0:<3}".format(APR), end="")
#printing interest rate loop
print("{0:>10}".format(""),"{0:>10}".format("3.25%"),"{0:>10}".format("3.75%"),
"{0:>10}".format("4.25%"),"{0:>10}".format("4.75%"),"{0:>10}".format("5.25%"),
"{0:>10}".format("5.75%"),"{0:>10}".format("6.25%"))
#never ending column headings
print("{0:>10}".format("# Payments"),"{0:>10}".format("="*len("3.25%")),"{0:>10}".format("="*len("3.75%")),
"{0:>10}".format("="*len("4.25%")),"{0:>10}".format("="*len("4.75%")),"{0:>10}".format("="*len("5.25%")),
"{0:>10}".format("="*len("5.75%")),"{0:>10}".format("="*len("6.25%")))
#attempting calculations
#payments column
for term in range(12,37,6):
print("{0:^10}".format(term))
#column 1
for term in range(12,37,6):

Categories