empmangpro = True
employees = []
while empmangpro == True:
try:
empcount = len(employees)
print("--------------------Employee Management System--------------------")
print("")
print("There are "+str(empcount)+" employees in the system.")
print("")
print("------------------------------------------------------------------")
print("1. Add new employee")
print("2. View all employees")
programselection = int(input("Please select your option number: "))
if programselection == 1:
employee = []
employee.append(input("First and Last Name: "))
employee.append(input("Social Security Number: "))
employee.append(input("Phone Number: "))
employee.append(input("Email Address: "))
employee.append(input("Salary:$"))
employees.append(employee)
elif programselection == 2:
i = 0
j = 0
empstr = ""
while i < int(empcount):
while j < 5:
empstr = empstr + employees[i][j]
if j != 4:
empstr = empstr + ", "
j += 1
if i+1 != int(empcount):
empstr = empstr + "n"
j = 0
i += 1
print(empstr)
print[employees]
elif programselection < 3:
empmangpro = False
else:
print("Please enter valid information")
except ValueError:
print("Please enter valid information")
continue
我有选项1工作,您可以在其中添加多个员工到系统,但当我选择选项2时,什么也没有发生。应该打印出我添加的所有员工,我哪里做错了?我只学了不到一个月的编程,所以我还有很多东西要学。我遗漏了什么或做错了什么?
不是很清楚你在选项2中要做什么。尝试在将来注释您的代码。你帖子里的标签不准确,所以我做了一些猜测。也许这会帮助你解决你的问题:
empmangpro = True
employees = []
while empmangpro == True:
try:
empcount = len(employees)
print("--------------------Employee Management System--------------------")
print("")
print("There are "+str(empcount)+" employees in the system.")
print("")
print("------------------------------------------------------------------")
print("1. Add new employee")
print("2. View all employees")
programselection = int(input("Please select your option number: "))
if programselection == 1:
employee = []
employee.append(input("First and Last Name: "))
employee.append(input("Soci1al Security Number: "))
employee.append(input("Phone Number: "))
employee.append(input("Email Address: "))
employee.append(input("Salary:$"))
employees.append(employee)
elif programselection == 2:
i = 0
j = 0
empstr = ""
while i < empcount:
print(str(employees[i])+"n")
i += 1
elif programselection > 2:
empmangpro = False
print("Stopping programm")
else:
print("Please enter valid information")
except ValueError:
print("Please enter valid information")
continue
如果您想停止程序,请使用elif programselection > 2:
而不是elif programselection < 3:
。
你说选项1有效。好的(我看到一些缩进问题)。
对于选项2,我将诚实地说,我没有完全遵循您的代码。如果你想要选项2打印所有员工,我建议创建一个Employee
类。这样,你可以在任何地方print(employee)
。
可以把这个放到employee.py
:
class Employee:
def __init__(self, name): # can add all you need social security, ...
first, last = name.split(' ')
self.first = first # add a new line per new parameter
self.last = last
def __str__(self):
# here you can add all of your logic
# to properly format social security, phone number, etc.
# just be sure to return a string, and not print a string!
return f'{self.last}, {self.first}'
然后在主文件中:
import employee # put at top of file
employees = [Employee('John Doe'), Employee('Jane Doe')] # sample data
for e in employees:
print(e)
输出:
Doe, John
Doe, Jane
对于空格错误:
if programselection == 1:
employee = []
# ...
employees.append(employee)
elif programselection == 2:
:
except ValueError:
print("Please enter valid information")
continue