Doing an assignment and can't get a variable passed between two methods in the same class.
global check
wage = 0
hours = 0
check = 0
class employee:
def __init__(self, name, wage, hours, check):
self.name = name
self.wage = wage
self.hours = hours
self.check = check
def paycheck(self):
if self.hours > 41:
self.hours = self.hours - 40
overtime = self.wage * self.hours * 1.5
self.check = (40 * self.wage) + overtime
return self.check
#print(self.check)
else:
self.check = self.hours * self.wage
return self.check
#print(self.check)
def __str__(self):
return self.name + ":\n Net Pay: $" + str(self.check)
A_emp = employee("Alice", 20, 40, 0)
B_emp = employee("Bob", 15, 50, 0)
print(A_emp)
print(B_emp)
The output should display the 'Net Pay' of each employee, and works to display most of the str method:
Alice:
Net Pay: $0
Bob:
Net Pay: $0
However, the check variable just wont pass values between class methods. I've globalized it, defined it outside of the class, and tried different variations of self.check vs check. Feel like I'm just throwing things and nothing is sticking. Thanks for any help.
You forgot to call the paycheck() method. Also get rid of your global variables and return statements.
class employee:
def __init__(self, name, wage, hours, check):
self.name = name
self.wage = wage
self.hours = hours
self.check = check
def paycheck(self):
if self.hours > 41:
self.hours = self.hours - 40
overtime = self.wage * self.hours * 1.5
self.check = (40 * self.wage) + overtime
else:
self.check = self.hours * self.wage
def __str__(self):
return self.name + ":\n Net Pay: $" + str(self.check)
A_emp = employee("Alice", 20, 40, 0)
B_emp = employee("Bob", 15, 50, 0)
A_emp.paycheck()
B_emp.paycheck()
print(A_emp)
print(B_emp)
Output:
Alice:
Net Pay: $800
Bob:
Net Pay: $825.0
Once you create the class object you need to call
A_emp.paycheck()
B_emp.paycheck()
Related
I'm learning about OOP and I need some help with defining one of the methods under Album, specifically total_runtime.
Here's some code (verified, all correct) on the context of the question.
class Duration(object):
def __init__(self, minutes, seconds):
self.total_seconds = minutes * 60 + seconds
self.minutes = int(self.total_seconds / 60)
self.seconds = self.total_seconds % 60
def get_minutes(self):
return self.minutes
def get_seconds(self):
return self.seconds
def __str__(self):
# returns the string representation of the Duration in "mm:ss" form.
if len(str(self.minutes)) < 2:
self.minutes = "0" + str(self.minutes)
if len(str(self.seconds)) < 2:
self.seconds = "0" + str(self.seconds)
return str(self.minutes) + ":" + str(self.seconds)
def __add__(self, duration):
#Adds 2 durations together
return Duration(self.minutes + duration.minutes, self.seconds + duration.seconds)
class Song(object):
def __init__(self, artist, title, duration):
self.artist = artist
self.title = title
self.duration = duration
def get_artist(self):
return self.artist
def get_title(self):
return self.title
def get_duration(self):
return self.duration
class Album(object):
def __init__(self, artist, title):
self.artist = artist
self.title = title
self.songs = list()
def add_song(self, song):
# Adds song (of class Song) to the album.
self.songs.append(song)
I need some help with defining the property total_runtime(self) under class Album which is supposed to return the total runtime (of class Duration) of the album.
Here's what I have now. I tried iterating through the album to get the durations of all the songs. Somehow I'm getting an error which says that add is not defined.
def total_runtime(self):
duration = (0,0)
for song in self.songs:
__add__(self, duration)
return duration
Would really appreciate any help debugging! Thank you!
You need to call __add__ as an attribute of the class, the self argument is the object you're calling from. It's implicitly moved into the arguments.
def total_runtime(self):
duration = Duration(0,0)
for song in self.songs:
duration.__add__(song.get_duration())
return duration
But really, __add__ is more cleanly used with the plus operator:
def total_runtime(self):
duration = Duration(0,0)
for song in self.songs:
duration += song.get_duration()
return duration
class Employee(object):
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '#company.com'
def fullname(self):
return '{} {}'.format(self.first, self.last)
def pay_raise(self):
self.pay = int(self.pay * 1.04)
emp_1 = Employee("Mark", "Johnson", 50000)
emp_1.pay_raise()
When I write that emp_1.pay_raise() phrase or emp_1.fullname() i dont get any results or any errors either after pressing "run" or "debug" in pycharm. Can you notice any mistakes in my code? I will very appereciate.
You are not printing out any of the "results" from executing the methods. Try this:
class Employee(object):
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '#company.com'
def fullname(self):
return '{} {}'.format(self.first, self.last)
def pay_raise(self):
self.pay = int(self.pay * 1.04)
emp_1 = Employee("Mark", "Johnson", 50000)
print(emp_1.fullname())
emp_1.pay_raise()
print(emp_1.pay)
def forecast(bank, years):
class Bank:
def __init__(self, name):
self.name = name
self.mark_cap = 0
self.acc_list = []
self.age = 0
def lend(self, principal, ann_inc):
self.mark_cap -= principal
def forward_year(self):
self.age += 1
def back_year(self):
if self.age == 0:
self.age = 0
self.age -= 1
def show_high(self):
print(Bank.acc_list[0])
class Account:
def __init__(self, ID, password):
self.ID = ID
self.password = password
if len(password) < 5:
print('Password must be at least 5 characters')
self.amount = 0
self.interest = 0.0175
self.acc_org = [ID, password, self.amount, self.interest]
def deposit(self, x):
self.amount += x
self.acc_org[2] = self.amount
def withdraw(self, y):
self.amount -= y
self.acc_org[2] = self.amount
def threshold(self):
if self.amount >= 1000000:
self.interest = 0.02
def comp_int(self, n):
self.threshold()
self.amount *= (1 + self.interest)**n
self.acc_org[2] = self.amount
def show_amount(self):
print(self.amount)
def add_2_bank(self, name):
bank_name = name
bank_name.acc_list.append(self.acc_org)
X = Bank('Bank of china')
Account1 = Account('12345', '12345')
Account1.deposit(200)
Account1.comp_int(2)
Account1.add_2_bank(X)
X.show_high()
The error that I am getting is that my 'Bank' object (X) has no attribute acc_list(). Someone please help me.
In the show_high method, modify Bank.acc_list to self.acc_list. Only static properties can be used like Bank.*.
I have only been programming for about a month and my question is this. Once I am done in the class defining the functions and class, how can I use the user input with the functions. Any help is appreciated the can shed a little light.
class employee:
def __init__(self,first,last,pay,hours):
self.first = raw_input("whats your first name")
self.last = raw_input("whats your last name")
self.pay = int(input("how much do you make an hour"))
self.hours = int(input("how many hours do you have"))
def raise_amount(self, amount):
self.pay = int(input('how much would you like to raise the employee pay'))
def overtime(self,overtime):
if self.hours !=39:
print ("there is an error you have overtime standby")
num1 = self.pay / 2
overtime = num1 + self.pay
print self.first, + self.overtime(self.hours)
print employee(self.hours)
As it stands this class does not make a lot of sense, particularly this bit:
class employee:
def __init__(self,first,last,pay,hours):
self.first = raw_input("whats your first name")
self.last = raw_input("whats your last name")
self.pay = int(input("how much do you make an hour"))
self.hours = int(input("how many hours do you have"))
By giving __init__ four arguments (in addition to self), it means that when you instantiate the class (via my_employee = employee(...)), you will have to pass all of those arguments, i.e. in your code you have to write my_employee = employee("John", "Cleese", "£2", "5 hours"). But that's pointless, because the __init__ function then completely ignores all of that information when it sets the attributes of the class, and instead uses user input. You just want to do this:
class employee:
def __init__(self):
self.first = raw_input("whats your first name")
self.last = raw_input("whats your last name")
self.pay = int(input("how much do you make an hour"))
self.hours = int(input("how many hours do you have"))
...
my_employee = employee()
It would be better however to create a general employee class, and then in circumstances where you need to create an employee via input, you can still do so. Specifically:
class Employee:
def __init__(self, first, last, pay, hours):
self.first = first
self.last = last
self.pay = pay
self.hours = hours
...
your_employee = Employee(input("First name: "), input("Last name: "),
int(input("Pay: ")), int(input("Hours: ")))
It works for Employee and calculate_wage, but returns an error when I try to create an instance of PartTimeEmployee and call to the calculate_wage method of PartTimeEmployee's parent class.
class Employee(object):
"""Models real-life employees!"""
def __init__(self, employee_name):
self.employee_name = employee_name
def calculate_wage(self, hours):
self.hours = hours
return hours * 20.00
class PartTimeEmployee(Employee):
def __init__(self, employee_name):
self.employee_name = employee_name
def calculate_wage(self, hours):
self.hours = hours
return hours * 12.00
def full_time_wage(self, hours):
return super(PartTimeEmployee, self).calculate_wage(self, hours)
milton = PartTimeEmployee("Milton")
print (milton.full_time_wage(10))
return super(PartTimeEmployee, self).calculate_wage(self, hours)
is incorrect, it should be
return super(PartTimeEmployee, self).calculate_wage(hours)
And next time: Also post the error message you're seeing.