首次发布,并且是python的新手(学习了3周)。当我试图更好地理解类时,这是一个作业生成器,它输出具有两个或更多不同名称的团队(列表):一个值超过3(或“ M”)而一个值更低。需要帮助的部分以2个星号开头。不断给我一个错误,说str对象没有属性“ name”。
import random
class Rankings:
ranks = ["","T","B","M","P","C"] #rankings going in order #from least to greatest with index being a respective value of #the ranking. Ex. C = 5, P = 4....
listofranks = []
num_of_emps =[]
def __init__(self,name,ranking):
self.name = name #mover’s name
Rankings.num_of_emps.append(self.name)#adds #mover to list once created
self.ranking = ranking #mover’s ranking
Rankings.listofranks.append(self.ranking) #adds ranking #to list once created
def rank(self):
self.ranking = Rankings.ranks[self.ranking]
return self.ranking #returns the mover’s respective rank #letter
def find_hi_rank(self):
**hirank = [self.name in Rankings.num_of_emps for i in Rankings.listofranks if i > 3 ]** #Obviously this part is #confusing as I don’t know what to do to get what I want. I #want it to print name for each item w value greater than 3
return hirank
@classmethod
def fromstring(cls,rank_str):
name,ranking = rank_str.split("-")
return cls(name,ranking)
Tim = Rankings("Tim", 5)
Luis = Rankings("Luis", 5)
Chris = Rankings("Chris", 4)
Fab = Rankings("Fab",3)
G = Rankings("G", 3)
Everett = Rankings("Everett", 3)
print(Rankings.num_of_emps)
numofjobs = 3
numofmovers = len(Rankings.num_of_emps)#how ever #many movers were added is the number of movers that can #be chosen from
movers = Rankings.num_of_emps
job = 0
while numofjobs >0:
job +=1
crew = int(input(f"How many movers are on job {job} ? "))
#if crew < movers:
#raiseError: Not enough movers
groups = []
numofmovers -= crew
numofjobs -=1
for i in range(crew):
if i == 0:
new = list(map(Rankings.find_hi_rank, movers))
",".split(new)# creates a list of movers that are #ranked higher than 3
if len(new) == 0:
break
first = random.choice(new)
groups.append(first)
else:
second = random.choice(movers)
groups.append(second)
movers.remove(second)
print("Movers:", groups)