簡體   English   中英

如何根據條件將用戶輸入排序到列表中

[英]How can i sort user input into a list based on conditionals

所以我堅持一個家庭作業問題,如果我到目前為止的內容是錯誤的,我只需要一些指導或批評。 但提示是“創建一個將創建服裝對象的程序。服裝對象必須具有以下屬性:最低溫度、最高溫度和正式程度。一旦你創建了一個衣服列表,輸入當前溫度和你要去的活動的正式程度。然后,根據正式程度輸出一個可以穿的衣服列表。

這是我到目前為止所擁有的,但我不知道我是否不能根據我的類實例將用戶輸入排序到列表中

class Clothing:
    def __init__(self, fit: str, mintemp: int, maxtemp: int, formalitylvl: str):
        self.fit = fit
        self.mintemp = mintemp
        self.maxtemp = maxtemp
        self.formalitylvl = formalitylvl

if __name__ == '__main__':

    listofclothes = []

    listofclothes.append(Clothing("coat", 0, 60, "yes"))
    listofclothes.append(Clothing("dress", 0, 100, "yes"))
    listofclothes.append(Clothing("T-shirt", 40, 100, "no"))
    listofclothes.append(Clothing("Hoodie", 0, 60, "no"))
    listofclothes.append(Clothing("jean shorts", 60, 100, "no"))

    catalog = int(input("please enter the number of clothing pieces you want to catalog: "))
    for i in range(catalog):
        str("please enter the clothing item: ")
        int(input("Please enter the minimmum temp you would wear the item:"))
        int(input("Please enter the maximum temp you would wear the item:"))
        str(input("Is the item formal: "))

    

你在正確的軌道上! 您應該將input保存到變量中,而不是在循環時重置輸入:

    type = input(str("please enter the clothing item: "))
    min_temp = int(input("Please enter the minimmum temp you would wear the item:"))
    max_temp = int(input("Please enter the maximum temp you would wear the item:"))
    is_formal = str(input("Is the item formal: "))

現在, loop衣服並檢查哪些是正確的:

output_list = []
for item in listofclothes:
    if (item.fit == type and item.mintemp == min_temp and item.maxtemp == max_temp and item.formalitylvl == is_formal):
        output_list.append(item)

我已經刪除了您的catalog范圍,因為您的作業中沒有提到它。 此外,這不是您應該循環的內容。 你應該遍歷衣服list

編輯:因為我忘記了服裝部分,所以添加了fit type

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM