Reputation: 505
I'm writing a program whose instructions specify "The str method must not contain any print statements and must return a string". So I've defined the function (within a the Employee class) as follows:
def __str__(self):
return "\nThe employee's name is " + self.__employeeName + \
"\nTotal regular hours worked: " + str(format(self.__regHours)) + \
"\nTotal overtime hours worked: " + str(format(self.__otHours)) + \
"\nTotal hours worked: " + str(format(self.__regHours + self.__otHours)) + \
"\nEmployee's pay rate: $" + str(format(self.__hourlyPayRate, ".2f")) + \
"\nMonthly regular pay: $" + str(format(self.getMonthlyRegPay(), ".2f")) + \
"\nMonthly overtime pay: $" + str(format(self.getMonthlyOtPay(), ".2f")) + \
"\nMonthly gross pay: $" + str(format(self.getGrossPay(), ".2f")) + \
"\nMonthly taxes: $" + str(format(self.getTaxes(), ".2f")) + \
"\nMonthly net pay: $" + str(format(self.getNetPay() - self.getTaxes(), ".2f"))
And I've created this instance of the Employee class in a separate file:
employee = EmployeeClass_New.Employee()
employee.setEmployeeName()
employee.setHourlyPayRate()
employee.setHoursWorked()
employeeName = employee.getEmployeeName()
hourlyPayRate = employee.getHourlyPayRate()
regHours = employee.getRegHours()
otHours = employee.getOtHours()
regPay = employee.getMonthlyRegPay()
otPay = employee.getMonthlyOtPay()
grossPay = employee.getGrossPay()
taxes = employee.getTaxes()
netPay = employee.getNetPay()
**print(f"{str(employee)}")**
repeat = input("Type 'yes' to input another employee or 'no' to quit: ")
My question pertains to the bolded part as I can only get this to output by printing it. I tried calling it like this:
employee.__str__()
but that didn't do anything...my question is, am I violating my professors instructions but doing this? Is there a way to call str that makes it produce output? All the articles I read look to print this method but I want to be very sure since my professor will give me a 0 for this assignment if I violate this rule.
Thanks so much!
Upvotes: 0
Views: 57
Reputation: 881403
The str method must not ...
The limitation is that the method itself must not contain print
statements. You have met that requirement, returning a string for some other piece of code to use as it sees fit.
Unless you using the REPL where non-None
expressions are printed auto-magically, you'll definitely have to print
the string outside of the method in order to see that it works. That is no way violates the requirements given, since it's not in the method.
Upvotes: 1