我正在使用外部文件,该文件的格式为:
-12345 CSEE 35000 Bart Simpson
-12346 CSEE 25000哈利波特
-12350 Economics 30000 Krusty The Clown
-13123 Economics 55000 David Cameron
第一项是ID,第二项是主题,第三项是工资,其余是人的名字。
在我的程序的一部分中,我试图打印在用户提交的值之间有工资的人的信息。我已将所有数据放在一个名为讲师的列表中,然后我将所有工资放在一个名为讲师薪水的单独列表中并尝试将它们整数化,因为起初我认为for循环不工作的原因是因为在尝试时从讲座循环中访问它们我觉得它们现在可能仍然是字符串的一部分。
我已经在我的程序中使用了一个循环来打印所有教授特定主题的人。该主题由用户提交。我试图再次使用for循环来获得工资,但它不起作用。
print""
# To God be the Glory
lecturer = []
lecturer_salary = []
x = 0
a = " "
print ""
String = raw_input("Please enter the lecturers details: ")
print ""
def printFormat(String):
String = String.split()
lastname = String[-1]
firstnames = " ".join(String[3:-1])
name = ", ".join([lastname, firstnames])
ID_Subject = " ".join(String[0:2])
money = String[2]
print "%s,%s %s %s" % (lastname,firstnames,ID_Subject,money)
printFormat(String)
while x < len(lecturer):
lecturer_salary.append(int(lecturer [x][2]))
x = x + 1
print ""
try:
fname = input("Enter filename within " ": ")
with open(fname) as f:
for line in f:
data = line.split()
printFormat(line)
line = line.split()
lecturer.append(line)
except IOError as e :
print("Problem opening file")
print ""
print ""
answer = raw_input("Would you like to display the details of lectureers from a particular department please enter YES or NO: ")
if answer == "YES" :
print ""
department = raw_input("Please enter the department: ")
print ""
while x < len(lecturer) :
for line in lecturer:
if lecturer[x][1] == department:
a = lecturer[x]
a = ' '.join(a)
printFormat(a)
x = x + 1
**elif answer == "NO" :
print ""
answer2 = raw_input ("Would you like to know all the lecturers within a particular salary range: ")
print ""
if answer2 == "YES":
lower_bound = int(input("Please enter the lower bound of the salary range: "))
upper_bound = int(input("Please enter the upper bound of the salary range: "))
print ""
while x < len(lecturer) :
for line in lecturer_salary:
if lower_bound < lecturer_salary[x] < upper_bound :
print lecturer_salary[x]
x = x + 1**
else:
print ""
print "Please enter a valid input"
答案 0 :(得分:0)
所以,你有一系列的讲师和一个讲师薪水。在
for line in lecturer_salary:
不需要- 只需要一会儿就可以了。请注意,这只会打印出薪水,而不是讲师的详细信息。由于x是两个数组的索引,因此您可以访问讲师[x]。事实上,你根本不需要讲师工资,只需通过讲师和检查:
while x < len(lecturer) :
if lower_bound < lecturer[x][2] < upper_bound :
a = lecturer[x]
a = ' '.join(a)
printFormat(a)
x = x + 1
答案 1 :(得分:0)
对于初学者,您不应使用大写字母String
或Id_Subject
来命名变量。
将代码分解为函数并尝试使用字典或类来提高可读性和可扩展性更为简单。
以下是使用class的最小代码:
lecturers = [] # To store Lecturer instances, which isn't necessary
class Lecturer():
def __init__(self, id, subject, salary, name):
self.id = id
self.subject = subject
self.salary = salary
self.name = name
def readfile(filename):
"""read each line in a file and yield a list of fields"""
with open(filename, "r") as f:
for line in f.readlines():
# return a list of fields
yield line.replace("\n", "").split()
def new_lecturer(detail):
"""Return a new lecturer instance from a list of fields"""
return Lecturer(detail[0],
detail[1],
detail[2],
{"firstname": detail[3],
"lastname": detail[4]
})
def print_lecturer_detail(lecturer):
"""Accept a lecturer instance and print out information"""
print "{0},{1} {2} {3}".format(lecturer.name["lastname"],
lecturer.name["firstname"],
lecturer.id,
lecturer.salary)
def main():
"""This is where all the main user interaction should be"""
fname = raw_input("Enter filename: ")
for lecturer in (readfile(fname)):
lecturers.append(new_lecturer(lecturer))
print ""
answer = raw_input("Would you like to display lecturers by department(Y/N)?: ")
if answer == "Y":
print ""
department = raw_input("Please enter the department: ")
print ""
for lecturer in lecturers:
if lecturer.subject == department:
print_lecturer_detail(lecturer)
elif answer == "N":
# implement salary code here
pass
if __name__ == '__main__':
main()
现在这可能是一种矫枉过正,但从长远来看,它比处理列表更好。你会发现处理属性变得更加简单。您可能希望进一步改进每个功能,使其更加模块化和可重复使用。
@Paul Morrington在while
部分有直接答案。