Reputation: 223
I fixed the issue with the spacing and have corrected other errors a long the way. Now it is doing what i want but what i select choice 2 it will print out the record of the employee 4 times. And if i enter another employee it will just print the second one and not the 1rst one as well.
class EmployeeClass:
def Employee(name, lastName, age, salary):
name = name
lastName = lastName
age = age
salary = salary
def displayEmployee(x):
print("Name: " + name + ", " + lastName)
print("Age: " + age)
print("Salary: " + salary)
EmployeeArray = []
Continue = True
print ("Employee Information V2.0")
while Continue == True:
print ("Welcome to Employee Information")
print ("1: Add New Record")
print ("2: List Records")
print ("3: Quit")
choice = input()
if choice == "1":
name = input ("Enter First Name: ")
EmployeeArray.append(name)
if name == "":
Continue = False
print ("Goodbye!")
break
lastName = input ("Enter Last Name: ")
EmployeeArray.append(lastName)
age = input ("Enter Age: ")
EmployeeArray.append(age)
salary = input ("Enter Salary: ")
EmployeeArray.append(salary)
elif choice == "2":
for Employee in EmployeeArray:
EmployeeClass.displayEmployee(Employee)
Continue = False
elif choice == "3":
print ("Bye!")
break
else:
print ("Please choose a valid option")
print ("\n")
Upvotes: 2
Views: 326
Reputation: 8947
It looks like you need to indent everything after the first line.
I.E.
class Employee:
empCount = 0
def _init_(self, name, lastName, age, salary):
...
Upvotes: 1
Reputation: 143102
Your error message will give you an indication about the line number where this problem is happening. Basically, you are mixing tabs and blank spaces, so you need to use only one of them consistently for indentation.
PEP8 - The Style Guide for Python recommends the use of spaces and also notes:
When invoking the Python command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!
From briefly examining your source it seems that there are tabs in front of the print
statements - replace those with blanks (that is also the reason they are not rendered correctly in the post above)
while Continue == True:
print ("Welcome to Employee Information")
print ("1: Add New Record")
print ("2: List Records")
print ("3: Quit")
There might be other spots, you'll have to check carefully. In fact I suspect where your code doesn't show as correctly indented in your post might be worth a look.
In the order to avoid problems like this it's best to use an editor or IDE that will consistently indent for you with the same characters.
Upvotes: 5
Reputation: 56674
The problem is that you are using a tab character ('\t') to indent some lines, and four spaces (' ') to indent others.
This can be a problem, because a tab doesn't necessarily mean a given number of spaces - different editors may interpret it as the equivalent of 2 or 4 or 8 spaces (or really, any number of spaces it likes, but those are the most common choices). If you indent using only spaces, or only tabs, that is unambiguous and Python is happy. If you use a mixture, the meaning of your code will be different depending on how many spaces a tab character is equal to - so instead of guessing, possibly leading to strange errors, Python stops with the warning message you see.
The immediate solution is to use search-and-replace to replace every tab character with 4 spaces. The longer-term solution is to use a text editor which will automatically insert 4 spaces whenever you hit the tab key. (On Windows, I like Notepad++, but there are lots of other good ones too).
Upvotes: 0
Reputation: 10575
Never use tabs in Python, you can but it's not conventional. The PEP8 convention is to use four spaces.
Upvotes: 0