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.
Related
Trying to find the num_multiples of n, starting from zero, and using recursion.
Correct Example:
Input: print_first_multiples(5, 10)
Output: 0 5 10 15 20 25 30 35 40 45
I'm able to print out the spacing correctly, but I'm unable to properly increment by n or start from 0.
My code below results in this:
Input: print_first_multiples(5, 10)
Output: 5 10 20 40 80 160 320 640 1280 2560
I know that my recursive call for n is wrong, but I'm at a complete loss as to what to do. If you're able to provide help without outright solving it for me, that would be greatly appreciated!
def print_first_multiples(n, num_multiples):
'''Prints out the integer multiples of n, starting with 0'''
if num_multiples <= 0:
print(end = " ")
else:
print(n, end = " ")
print_first_multiples(n+n, num_multiples-1)
I know my recursive call for n is wrong, but I'm at a complete loss as to what to do. If you're able to provide help without outright solving it for me, that would be greatly appreciated!
Your recursion needs an extra parameter to be able to figure out which multiple of n is going to be printed.
I would use the following method declaration: print_first_multiples(n, num_multiples, cur_multiple=0).
I am gathering data on a device, and after every second, I update a count and log it. I am now processing it, and am new to python, so I had a question as to whether it was possible to convert a numbered array [0,1,2,3,4,...1091,1092,1093,...] into a timestamp [00:00:01, 00:00:02, 00:00:03, 00:00:04, ... 00:18:11, 00:18:12, 00:18:13,...] for example.
If you could please lead me in the right direction, that would be very much appreciated!
p.s. In the future, I will be logging the data as a timestamp, but for now, I have 5 hours' worth of data that needs to be processed!
import datetime as dt
timestamp=[0,1,2,3,4,5,1092,1093]
print([dt.timedelta(seconds=ts) for ts in timestamp])
Happy Coding
If all you have is seconds, then you can just do simple arithmetic to convert them to minutes and hours:
inp = [0, 1, 2, 3, 4, 1091, 1092, 1093]
outp = [f'{secs // 3600:02}:{(secs // 60) % 60:02}:{secs % 60:02}' for secs in inp]
print(outp)
# ['00:00:00', '00:00:01', '00:00:02', '00:00:03', '00:00:04', '00:18:11', '00:18:12', '00:18:13']
Here, I use a list comprehension and, for each secs in the input, create a format string:
hours is secs // 3600 (that's integer floor division), because one hour is 3600 seconds
Minutes is (secs // 60) % 60 (this incorporates the modulo operator, which displays the remainder of secs // 60 after dividing it by 60 again). One minute is 60 seconds, but more than 60 minutes would be an hour, so we need to make sure to 'roll over' the counter every 60 minutes (which is what the mod is for).
Seconds is, of course, secs % 60, because a minute has 60 seconds and we want the counter to roll over.
The format string starts with f', and anything inside {} is an instruction to evaluate whatever's inside it, and insert that into the string. The syntax is {expression:format}, where display is an optional instruction for how to format the data (i.e. not just printing it out). And format can get complicated (look up a python f-string tutorial if you're curious about the specifics), but suffice it to say that in this case we use 02, which means that we want the output to be two characters in length, and padded with zeroes in case it's less than that.
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)
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))
Within my program I have a column(H) specifying hours:
(24.2, 23.5, 21.5, 25.0, 28.3, 23.1, 22.5, 17.9, 22.1, 16.2, 24.3, 23.8)
this continues for 600 or so more rows.
Max hours = 36.88348
Min hours = 16.15569
I'm trying to categorise the hours into four different numbers to later use for more accurate data than averages:
0 = 16-20,
1 = 21-25,
2 = 26-30,
3 = 31>.
So far I have came to this solution:
=IF($H4>=31,3,IF($H4<=20,0,IF($H4>=21<=25,1,IF($H4>=26,2))))
This works apart from the 21-25($H4>=21<=25,1).
If anybody could assist me, I believe it's something basic as my syntax.
Shorter still:
=MATCH(H4,{0,21,26,31})-1
Slightly shorter:
=LOOKUP(H4,{0,21,26,31},{0,1,2,3})
Try this:-
=IF($H4>=31,3,IF($H4<=20,0,IF(AND($H4>=21,$H4<=25),1,IF($H4>=26,2))))
Just start with the lowest value and work up:
=IF($H4<=20,0,(IF($H4<=25,1,(IF($H4<=30,2,3)))))
The following should work:
=IF($H4>=31,3,IF($H4>=26,2,IF($H4>=21,1,IF($H4<21,0))))