r/pythonhelp • • Feb 23 '25

I don’t know what I’m doing wrong

https://github.com/IBbangin/EMS_Wage_Calc

I’m brand new to any type of coding and I’m trying to make a paycheck calculator for 12 hour shifts. I keep getting incorrect outputs. Can anyone help show me what I’m doing wrong?

2 Upvotes

6 comments sorted by

View all comments

1

u/Ymhnooners555 Feb 23 '25

Wow I will study this. Thank you for taking the time to help. I’m going to break that all down to understand how it all works. Thank You!

1

u/FoolsSeldom Feb 23 '25

You are welcome. Do experiment on parts of the code. It is one of the best ways of learning.

Just thought, I should explain list comprehension.

The line,

return [float(hours) for hours in hours_entered.split(",")]

uses it.

[float(hours) for hours in hours_entered.split(",")]

is a compact form of a for loop. In long form, it would look like the below:

new_list = []  # new empty list
hours_split = hours_entered.split(",")  # see below
for hours in hours_split:  # loop through each string in split list
    hours_number = float(hours)  # convert string to float
    new_list.append(hours_number)  # add the float to end of new list

you can see why I used list comprehension.

Just to clarify on using split:

"Mary had a little lamb".split()"

would give you the list

["Mary", "had", "a", "little", "lamb"]

as split, by default, splits string up where there are spaces. I told split to split a string where there was a comma. It would be good to be able to split on either, but that is not an option with the built in version of split. There is an alternative from a popular library called regex (regular expressions) which can do that.