How to make a variable after a certain integer in python - python-3.x

Im brand new to programing this is literally my second day just looking at this stuff. So please forgive me if this is answered somewhere already or if its a really dumb question, this is still like witch craft to me.
Anyways I have to make a pay calculator and I want to make a variable for anything after 40 to be overtime. This is what I have so far.
input("Enter Name, Or 0 To Quit: ")
hour = float(input("Hours: "))
if hour > 40:
overTime = float(print("Over Time Hours: "))
payRate = float(input("Pay Rate: $ "))
overTime = overTimeHour * 1.5
grossPay = hour * payRate + overTime
print("\nGross Pay: $", (grossPay))
else:
payRate = float(input("Pay Rate: $ "))
grossPay = hour * payRate
print("\nGross Pay: $",(grossPay))

I don't think you want your overTime variable to be a float(print()). That'll probably throw an error. You already have the total number of hours, so isn't the number of overtime hours just hours - 40? I don't think you need another input for that. Then, you need to change up your formula for gross pay a bit.
I also moved the input for payRate out, since it applies to both conditions in the if statement.
The following code should do the trick:
input("Enter Name, Or 0 To Quit: ")
hour = float(input("Hours: "))
payRate = float(input("Pay Rate: $ "))
if hour > 40:
overTimeHours = hour - 40
# This can be simplified (via commutative property) if you'd like
grossPay = (40 * payRate) + (overTimeHours * 1.5 * payRate)
print("\nGross Pay: $", (grossPay))
else:
grossPay = hour * payRate
print("\nGross Pay: $",(grossPay))

Related

If statement not met, even though it should be

I am making an overtime pay calculator in rust. I have the following code below:
let overhours = 40;
let overhoursFLOAT = overhours as f32;
if calcpay > overhours{
println!("You worked more than 40 hours!");
let overmath = floathours - overhoursFLOAT as f32;
println!("You worked {} extra hours.",overmath);
let overpay = overmath * 1.5;
floatpay = floatpay + overpay;
}
else{println!("You did not work more than 40 hours. Therefore, your overtime pay will not be calculated.");}
Everytime i run it, the hours i input (I do 10) is under 40, and the if statement code runs anyway. It ends up looking like this:
How much do you get paid per hour?: 9
Hourly pay: 9
How much did you work?: 10
Hourly pay: 9
Hours worked: 10
Your calculated pay is $90
You worked more than 40 hours!
You worked -30 extra hours.
Am i using the operators wrong? How can i fix this?
you are comparing calcpay with overhours and calcpay equals 90. I assume what you wanted is to compare overhours with hours worked instead.

How to improve readability of code that transforms seconds to hours, minutes and seconds format

I got the code to do what I want, yet I feel its hardcoded than an actual solution. Any suggestions on how I could adjust the Hours, Minutes, Seconds variables to be more clear to the reader?
Input = int(input("Seconds: "))
Hours = Input // (60*60)
Minutes = Input//(60) - (Hours*60)
Seconds = Input - (Minutes*60) - (Hours*60*60)
print(Hours,"Hours",Minutes,"Minutes",Seconds,"Seconds")
Use modulos instead of division. They're a little confusing at first, but they're really awesome.
def convert(seconds):
secs = seconds%60
mins = (seconds//60)%60
hrs = (seconds//3600)
return (secs,mins,hrs)
From a code optimization standpoint, my code does a total of four arithmetic operations, whereas yours runs through 10. Additionally, the whole (Hours * 60) thing is a little difficult to understand.
That's not to say your code is bad, just a little unclear. Though readability counts, your code is not so illegible as to be impossible to understand.
Constants help readability. Also using modulo helps:
SEC_PER_MIN = 60
SEC_PER_HOUR = SEC_PER_MIN * 60
secs = int(input("Seconds: "))
hours = secs // SEC_PER_HOUR
remaining_seconds = secs % SEC_PER_HOUR
mins = remaining_seconds // SEC_PER_MIN
remaining_seconds %= SEC_PER_MIN
print(f"{hours} Hours, {mins} Minutes, and {remaining_seconds} Seconds")
or you can abuse the time module and have it handle all the logic:
import time
secs = int(input("Seconds: "))
result = time.strftime('%H Hours, %M Minutes, %S Seconds', time.gmtime(secs))
print(result)

Plot time vs angle; how to convert time into a number

I am trying create a plot(angle of incidence vs. time). Time is set between hour of sunrise to hour of sunrise (6:37:00 AM - 6:39:00 PM). I have to find angle of incidence for each minute interval starting from sunrise to sunset. The only issue is I don't have the faintest clue how to convert time into a number.
Angle of incidence depends on hour angle (Angle_hour). This is dependent on the time. Time before noon is given a negative value, and time after noon is positive. For example, at 6:37 am, the Hours would equal -6.62. On the other hand, 6:39 PM would equal 6.65. I am trying to have a for loop calculate the different values within the time frame.
for k = 1:6
Hours = k;
Angle_Hour(k) = 15 * Hours;
Angle_Incidence(k) = acos((sin(Angle_Declination) * sin (Angle_Latitude) * cos(Angle_Slope)) - (sin(Angle_Declination) * cos(Angle_Latitude) * sin(Angle_Slope) * cos(Angle_SurfaceAzimuth)) + (cos(Angle_Declination) * cos(Angle_Latitude) * cos(Angle_Slope) * cos(Angle_Hour(k))) + (cos(Angle_Declination) * sin(Angle_Latitude) * sin(Angle_Slope) * cos(Angle_SurfaceAzimuth) * cos(Angle_Hour(k))) + (cos(Angle_Declination) * sin(Angle_Slope) * sin(Angle_SurfaceAzimuth) * sin(Angle_Hour(k)))) ;
end
If in your program the time in a day is a variable of type datetime, then you can either use datenum to turn the date to a number, or you can use the functions: hour, minute, second to extract the hours, minutes and seconds, and then calculate the angle using them.
So for example, you can have something like this:
function angle = Angle_Hour(k)
hours = hour(k) + minute(k)/60 + second(k)/3600
angle = % some expression/function of time in hours
end

simple and compound annual investment calculator in python3

I am trying to make a simple compound and annual investment calculator, but there is an error that I have been unable to spot. Am I missing a for or while loop?
investment=float(input("Enter an initial investment."))
interest=float(input("Enter an interest rate between .01 to .10."))
years=float(input("Enter a number of years between 1 to 30."))
t=(investment) * (interest+1) ** (years)
print(t)
Future Value Calculation
print("Investment Calculator")
print(f"=====================")
present_value = int(input("Principal Amount? "))
interest_rate = float(input("Annual Interest Rate? "))
terms_in_years = int(input("Number of Years? "))
future_value = present_value * pow((1 + (interest_rate / 100)), terms_in_years)
result = float("{:.2f}".format(future_value))
print(f"Future Value is: {result}")"
The numbers look good to me .... (1000 + .01 + 10):
def formatInterest(t):
return "{:>3} {:<10}".format(t[0],round(t[1],2))
investment=float(input("Enter an initial investment."))
interest=float(input("Enter an interest rate between .01 to .10."))
years=int(input("Enter a number of years between 1 to 30."))
print(*list(map(formatInterest,enumerate([investment * (interest+1) ** i
for i in range(years+1)]))),sep="\n")
Output:
0 1000.0
1 1010.0
2 1020.1
3 1030.3
4 1040.6
5 1051.01
6 1061.52
7 1072.14
8 1082.86
9 1093.69
10 1104.62
You can read about map(), enumerate() and print() here:
map()
enumerate()
print
The list comprehension produces the values for each year, starting with 0 up to 10 (the input). The enumerate adds the year-number for the formatInterest-input-tuple. map applies the formatting and print() prints it all, using a sep of newline on the decomposed list.

Python Print Displays

I am having a problem with this project that I'm working. My problem that I am having is that at the end of my Display list I am getting a negative number, so I wondering if someone could give me any direction on how to fix this problem? I need it to be zero.
InitalPrice = float(input("Enter the Price of the Computer: "))
Month = 0
DownPayment = InitalPrice * .10
Balance = (InitalPrice - DownPayment)
AnInterest = Balance * .01 / 12
MonthlyPayment = Balance * 0.05
print("%0s%20s%20s%20s%13s%23s" %("Month", "Current Balance", "Interest
Owed", "Principal Owed", "Payment", "Balance Remaining"))
for i in range(1, 100):
#AnInterest = AnInterest
if Balance >= 0:
InitalPrice = InitalPrice - InitalPrice * .10
Principal = MonthlyPayment - AnInterest
Balance = Balance + AnInterest - MonthlyPayment
print("%0d%20.2f%20.3f%20.2f%13.2f%23.2f" %(i, InitalPrice, AnInterest, Principal, MonthlyPayment, Balance))
You are subtracting a constant amount as the MonthlyPayment (ie. 10% of your balance). If the balance plus the interest is less then that MonthlyPayment then the balance becomes negative. You just have to do a simple check to see that this is not the case.
P.S. I would recommend just using a while loop for this problem as the for loop can either iterate too many times, or not enough times.

Resources