我真的不知道如何描述這一點,但在我可怕的解釋之后,我會在預格式化的文本中包含一個輸出。所以...我創建了三個串列,我需要使用兩個,'employeeSold' 和 'employeeName',但是我需要合并/對齊這兩個,這樣我就可以建立一個關于誰獲得最多的排名系統,我想出了如何訂購對于employeeSold 從最大到最重要,但我最不確定如何將正確的名稱鏈接到正確的值,我認為它需要在訂購之前出現,但我真的不知道該輸入什么,我已經花了幾個小時的思考。
這是我想要此代碼的示例輸出:
Ranking:
John - 5
Laura - 4
BOJO -1
它將每個整數 (employeeSold) 分配給每個字串 (employeeName),然后將用戶插入的整數 (employeeSold) 從最大到最重要排序,然后在整數旁邊列印它們的名稱。
這是我的代碼:
def employee():
employeeName = input("What is Employee's name?: ")
employeeID = input("What is Employee's ID?: ")
employeeSold = int(input("How many houses employee sold?: "))
nameList.append(employeeName)
idList.append(employeeID)
soldList.append(employeeSold)
nextEmployee = input("Add another employee? Type Yes or No: ")
if nextEmployee == "2":
employee()
else:
print("Employee Names:")
print(", ".join(nameList))
print("Employee's ID: ")
print(", ".join(idList))
print("Employee Sold:")
print( " Houses, ".join( repr(e) for e in soldList ), "Houses" )
print("Commission: ")
employeeCommission = [i * 500 for i in soldList]
print(", ".join( repr(e) for e in employeeCommission ), "" )
print("Commission Evaluation: ")
totalCommission = sum(employeeCommission)
print(totalCommission)
soldList.sort(reverse=True)
print("Employee Ranking: ")
ranking = (", ".join( repr(e) for e in soldList))
print(ranking)
nameList = []
idList = []
soldList = []
employee()```
--------
Any help would be very much so appreciated.
uj5u.com熱心網友回復:
您可以考慮將您的邏輯分解為多個函式,并對, 和list的zip進行排序以列印您指定的排名:namesnum_houses_sold
def get_int_input(prompt: str) -> int:
num = -1
while True:
try:
num = int(input(prompt))
break
except ValueError:
print('Error: Enter an integer, try again...')
return num
def get_yes_no_input(prompt: str) -> bool:
allowed_responses = {'y', 'yes', 'n', 'no'}
user_input = input(prompt).lower()
while user_input not in allowed_responses:
user_input = input(prompt).lower()
return user_input[0] == 'y'
def get_single_employee_info(names: list[str], ids: list[int],
num_sold_houses: list[int]) -> None:
names.append(input('What is the employee\'s name?: '))
ids.append(get_int_input('What is the employee\'s id?: '))
num_sold_houses.append(
get_int_input('How many houses did the employee sell?: '))
def get_houses_sold_info(names: list[str], ids: list[int],
num_sold_houses: list[int]) -> None:
get_single_employee_info(names, ids, num_sold_houses)
add_another_employee = get_yes_no_input('Add another employee [yes/no]?: ')
while add_another_employee:
get_single_employee_info(names, ids, num_sold_houses)
add_another_employee = get_yes_no_input(
'Add another employee [yes/no]?: ')
def print_entered_info(names: list[str], ids: list[int],
num_sold_houses: list[int]) -> None:
print()
row_width = 12
comission_per_house = 500
header = ['Name', 'Id', 'Houses Sold', 'Commission']
print(' '.join(f'{h:<{row_width}}' for h in header))
commission = [n * comission_per_house for n in num_sold_houses]
for values in zip(*[names, ids, num_sold_houses, commission]):
print(' '.join(f'{v:<{row_width}}' for v in values))
print()
total_commission = sum(commission)
print(f'Total Commission: {total_commission}')
print()
rankings = sorted(zip(num_sold_houses, names), reverse=True)
print('Ranking:')
for houses_sold, name in rankings:
print(f'{name} - {houses_sold}')
def main() -> None:
print('Houses Sold Tracker')
print('===================')
names, ids, num_houses_sold = [], [], []
get_houses_sold_info(names, ids, num_houses_sold)
print_entered_info(names, ids, num_houses_sold)
if __name__ == '__main__':
main()
示例用法:
Houses Sold Tracker
===================
What is the employee's name?: Laura
What is the employee's id?: 1
How many houses did the employee sell?: a
Error: Enter an integer, try again...
How many houses did the employee sell?: 4
Add another employee [yes/no]?: yes
What is the employee's name?: John
What is the employee's id?: 2
How many houses did the employee sell?: 5
Add another employee [yes/no]?: y
What is the employee's name?: BOJO
What is the employee's id?: 3
How many houses did the employee sell?: 1
Add another employee [yes/no]?: no
Name Id Houses Sold Commission
Laura 1 4 2000
John 2 5 2500
BOJO 3 1 500
Total Commission: 5000
Ranking:
John - 5
Laura - 4
BOJO - 1
順便說一句,如果你曾經做過一些真實的事情,不要使用連續的用戶 ID,你可以通過監控獲得的資訊量是瘋狂的,例如,用戶獲取率,你不希望外部人員成為能夠弄清楚。
uj5u.com熱心網友回復:
您可以使用 zip 功能合并串列,然后自定義鍵排序。SortByName 函式回傳排序鍵名稱和 SortBySalary 函式回傳鍵工資
def SortByName(FullList):
return FullList[0]
def SortBySalary(FullList):
return FullList[2]
NamesList=['John','Laura','Bojo']
IdList=[1,2,5]
SalaryList=[2000,1000,5000]
ZippedList=zip(NamesList,IdList,SalaryList)
print ZippedList
# result
#[('John', 1, 2000), ('Laura', 2, 1000), ('Bojo', 5, 5000)]
ZippedList.sort(key=SortByName,reverse=True)
print ZippedList
# result
# [('Laura', 2, 1000), ('John', 1, 2000), ('Bojo', 5, 5000)]
ZippedList.sort(key=SortBySalary,reverse=True)
print ZippedList
# result
# [('Bojo', 5, 5000), ('John', 1, 2000), ('Laura', 2, 1000)]
如果您以后想按 id 對串列進行排序,只需實作 id 排序功能。
def SortByID(FullList):
return FullList[1]
ZippedList.sort(key=SortByID)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/350273.html
上一篇:c#單鏈表這是如何作業的
