0

I'm working on a small personal project in Python 3 where you put in a name and get there weekly wages. I want to add a feature where an admin can come in and add a person to the workers, including their wages. Here's what I have as for people so far:

worker = input("Name: ")
if worker == "Anne":
    def calcweeklywages(totalhours, hourlywage):
        '''Return the total weekly wages for a worker working totalHours,
        with a given regular hourlyWage.  Include overtime for hours over 40.
        '''
        if totalhours <= 40:
            totalwages = hourlywage * totalhours
        else:
            overtime = totalhours - 40
            totalwages = hourlywage * 40 + (1.5 * hourlywage) * overtime
        return totalwages


    def main():
        hours = float(input('Enter hours worked: '))
        wage = 34
        total = calcweeklywages(hours, wage)
        print('Wages for {hours} hours at ${wage:.2f} per hour are ${total:.2f}.'
              .format(**locals()))


    main()
elif worker == "Johnathan":
    def calcweeklywages(totalhours, hourlywage):
        '''Return the total weekly wages for a worker working totalHours,
        with a given regular hourlyWage.  Include overtime for hours over 40.
        '''
        if totalhours <= 40:
            totalwages = hourlywage * totalhours
        else:
            overtime = totalhours - 40
            totalwages = hourlywage * 40 + (1.5 * hourlywage) * overtime
        return totalwages


    def main():
        hours = float(input('Enter hours worked: '))
        wage = 30
        total = calcweeklywages(hours, wage)
        print('Wages for {hours} hours at ${wage:.2f} per hour are ${total:.2f}.'
              .format(**locals()))


    main()

I want to add a part where if someone types in a code or something that they're an admin, it will let them add a person or edit an existing persons information.

2
  • 1
    ...There needs to be a better way. Executing input from the user is usually a bad idea. Like, for example, someone could delete your entire hard drive. I'm pretty sure even "admins" shouldn't be doing that. Perhaps an alternative method would be to create a config file in a place where only admins can get to it (on Windows at least this is easy) or perhaps ask for individual info (name, wages, etc) and then save that in a file, instead of actually editing the code (maybe you could enforce a password?...) Commented Mar 17, 2019 at 23:31
  • I think "code" is referring to a password here, not actual programming code. The general topic of user validation is a bit too broad. Commented Mar 18, 2019 at 0:09

1 Answer 1

1

I am not sure how you intend to deploy it, but even from the coding standpoint as it stands it will not behave the way you expect it to. I am guessing you are doing this just to learn. So, let me point out a couple of places where you have understood the basics wrong and a purely pedagogic example of how it could be done. Bear in mind that I strongly discourage from using this in any real context.

You have defined the function calcweeklywages twice in your code. When in fact it has to be defined just once. If you want to use the code, you call it, like you have done so in the main() program. The function works exactly the same for both your workers, so to get different weekly wages you pass different wages. But, how do you link their respective wages to their names (or some representation of them in your code)?

This is a good example where object oriented programming is used. A brief and entertaining primer is here. As for the code, it will look like this,

class Employee:
    def __init__(self, Name, Wage = 0, Hours = 0):
        self.Name = Name
        self.Wage = Wage
        self.Hours = Hours

def calcweeklywages(Employee, totalhours):
    '''Return the total weekly wages for a worker working totalHours,
    with a given regular hourlyWage.  Include overtime for hours over 40.
    '''
    hourlywage = Employee.Wage
    if totalhours <= 40:
        totalwages = hourlywage * totalhours
    else:
        overtime = totalhours - 40
        totalwages = hourlywage * 40 + (1.5 * hourlywage) * overtime
    return totalwages

# In your main body, you just test the functionality
EmployeeList = []
EmployeeList.append(Employee("Anne", 34))
EmployeeList.append(Employee("Johnathan", 30))

while(True):
    action = input('Exit? (y/n): ')
    if(action == 'y'):
        break
    else:
        name = input('Enter the employee\'s name: ')
        for Employee in EmployeeList:
            if(Employee.Name == name):
                Person = Employee
        hours = int(input('Enter the number of hours worked: '))
        print('Wages for', hours, 'hours at', Person.Wage,'per hour is', calcweeklywages(Person, hours))

EDIT: I am sorry, I forgot about the admin part. But here goes,

class Employee:
    def __init__(self, Name, Wage = 0, Hours = 0, Admin = False, code = ''):
        self.Name = Name
        self.Wage = Wage
        self.Hours = Hours
        self.Admin = Admin
        self.code = code

def calcweeklywages(Employee, totalhours):
    '''Return the total weekly wages for a worker working totalHours,
    with a given regular hourlyWage.  Include overtime for hours over 40.
    '''
    hourlywage = Employee.Wage
    if totalhours <= 40:
        totalwages = hourlywage * totalhours
    else:
        overtime = totalhours - 40
        totalwages = hourlywage * 40 + (1.5 * hourlywage) * overtime
    return totalwages

# In your main body, you just test the functionality
EmployeeList = []
EmployeeList.append(Employee("Anne", 34))
EmployeeList.append(Employee("Johnathan", 30))
EmployeeList.append(Employee("Mr. Admin", 50, 0, True, 'Open Sesame'))
while(True):
    action = int(input('Enter action :\n 1. Exit.\n 2. Add new employee.\n 3. Compute weekly wage\n'))
    if(action == 1):
        break
    elif(action == 2):
        AdminName = input('Enter operator name : ')
        Flag = False
        for EmployeeInst in EmployeeList:
            if((EmployeeInst.Name == AdminName) & (EmployeeInst.Admin)):
                code = input('Enter code :')
                if(code != EmployeeInst.code):
                    break
                NewName = input('New Employee name? :')
                NewWage = int(input('New employee wage? :'))
                EmployeeList.append(Employee(NewName, NewWage))
                Flag = True
        if(not Flag):
            print('Wrong Credentials')
            break
    elif(action == 3):
        name = input('Enter the employee\'s name: ')
        for Employee in EmployeeList:
            if(Employee.Name == name):
                Person = Employee
        hours = int(input('Enter the number of hours worked: '))
        print('Wages for', hours, 'hours at', Person.Wage,'per hour is', calcweeklywages(Person, hours))
    else:
        print('Input out of range')
        break

But again, the session is not persistent between different kernel runs. There is no real "security", this is just an exploration of Python's object oriented code. Please do not use this for any real application. There is a lot more that goes with all this. You need to store it in a secure file, have some GUI front end etc etc. There are far wiser users who will guide you to implement the system as a whole. All the best with your studies. Cheers.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! this looks like it will definitely help! I don't think I see a way for someone to add a person or their wage, and that's a feature i was kind of interested in adding. however, I am relatively new to coding with python so if it is included in what you very helpfully provided, i apologize and ask if it could be pointed out as to where it is. Thank you again!
Thank you! this looks like exactly what I'm looking for, and of course this wouldnt be used in an actual setting, I'm just interested in seeing what python does and experimenting

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.