Aug-23-2023, 06:21 PM
Your description of the error is confusing. Are you using IDLE?
IDLE is a bad tool. IDLE works differently than other Python IDE's or running Python from the shell. You can never be really sure if odd behaviors are due to an issue is in your code, or if it is another of IDLE's idiosyncrasies.
This is what happens when I run your code in Python. I copied your program to a file named test.py
To make something that works like IDLE, I need to run the python program and use the "-i" option to tell Python to leave the interpreter open after executing the file. Since I am executing the file, I'll remove the test code at the bottom. I am tired of Michael.
Unless you are writing a class designed for input or output, it is unusual to have the class do input or output. Somebody using your class probably doesn't want it to print "Creating Staff object" each time they create an instance of your class. It is unlikely they will use the calculatePay() method because they probably have hours for the employee in a file or database and don't want to type in the values. I would write your class more like this:
IDLE is a bad tool. IDLE works differently than other Python IDE's or running Python from the shell. You can never be really sure if odd behaviors are due to an issue is in your code, or if it is another of IDLE's idiosyncrasies.
This is what happens when I run your code in Python. I copied your program to a file named test.py
Output:(venv) C:\Users...\python_sandbox>python test.py
(venv) C:\Users\...\python_sandbox>
Not much interesting there. The file defines a class but doesn't do anything. I add some statements to make it a bit more interesting. The extra staments only execute when this file is executed. They do not execute when this file is imported by another file.class Staff: def __init__(self, pPosition, pName, pPay): self.position = pPosition self.name = pName self.pay = pPay print("Creating Staff object") def __str__(self): return "Position = %s, Name = %s, Pay = %d" % ( self.position, self.name, self.pay, ) def calculatePay(self): prompt = "\nEnter number of hours worked for %s: " % (self.name) hours = input(prompt) prompt = "Enter the hourly rate for %s: " % (self.name) hourlyRate = input(prompt) self.pay = int(hours) * int(hourlyRate) return self.pay if __name__ == "__main__": # A little test program5 officeStaff1 = Staff("Basic", "Michael", 1) print(officeStaff1.name) print(officeStaff1.position) print(officeStaff1.calculatePay())
Output:(venv) C:\...\python_sandbox>python test.py
Creating Staff object
Michael
Basic
Enter number of hours worked for Michael: 5
Enter the hourly rate for Michael: 2
10
(venv) C:\...\python_sandbox>
Another way I can use his code is to start an interactive python interpreter, import the module, and type in python statements.Output:(venv) C:\...\python_sandbox>python
Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep 5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from test import Staff
>>> jeff = Staff("Manager", "Jeff", 5)
Creating Staff object
>>> jeff.name
'Jeff'
>>> jeff.position
'Manager'
>>> jeff.pay
5
>>> jeff.calculatePay()
Enter number of hours worked for Jeff: 4
Enter the hourly rate for Jeff: 5
20
>>> quit()
Notice that Michael was not automatically created when I imported the file. This is because __name__ only equals "__main__" when executing a Python file. When importing a file, __name__ == filename of the imported module.To make something that works like IDLE, I need to run the python program and use the "-i" option to tell Python to leave the interpreter open after executing the file. Since I am executing the file, I'll remove the test code at the bottom. I am tired of Michael.
Output:(venv) C:\...\python_sandbox>python -i junk.py
>>> jeff = Staff("Manager", "Jeff", 5)
Creating Staff object
>>> jeff.name
'Jeff'
>>> jeff.position
'Manager'
>>> jeff.pay
5
>>> jeff.calculatePay()
Enter number of hours worked for Jeff: 4
Enter the hourly rate for Jeff: 5
20
>>> quit()
Some comments about your class:Unless you are writing a class designed for input or output, it is unusual to have the class do input or output. Somebody using your class probably doesn't want it to print "Creating Staff object" each time they create an instance of your class. It is unlikely they will use the calculatePay() method because they probably have hours for the employee in a file or database and don't want to type in the values. I would write your class more like this:
class Staff: def __init__(self, position, name, rate): self.position = position self.name = name self.rate = rate def __str__(self): return f"{self.name}, {self.position}, Rate = {self.rate}" def pay(self, hours, rate=None): return hours * self.rate if rate is None else rate if __name__ == "__main__": emp = Staff("CEO", "Guido", 42) print(f"{emp}. Pay for 10 hours = {emp.pay(10)}")And now I can use that class in another program.
from staff import Staff # Renamed generic class module "staff.py" name, position, rate = input("Enter Employee Name, position, wage: ").split(",") emp = Staff(position.strip(), name.strip(), float(rate)) hours = float(input(f"Enter hours for {emp.name}: ")) print(f"{emp} pay = {emp.pay(hours)}")
Output:(venv) C...python_sandbox>python test_staff.py
Enter Employee Name, position, wage: Guido, CEO, 42
Enter hours for Guido: 5
Guido, CEO, Rate = 42.0 pay = 210.0