簡體   English   中英

為什么我的 class 程序不工作? (Python)

[英]Why isn't my class program working? (python)

我正在嘗試使用 function 語句創建 class,這樣我就可以學習如何編寫更簡潔的代碼。 當我在我的主程序中定義它時,我不斷收到“userInput”未定義。 為什么?

"""

class ShippingCharges:
    def __init__(self, userInput=None):
        self.userInput = userInput


    def getPrice (self):
        if (userInput <= 2):
            return("Package", packNum," will cost $1.10 per pound")
        elif(6 < userInput > 2):
            return("Package", packNum, " will cost $2.20 per pound")
        elif(10 < userInput >6):
            return("Package", packNum, " will cost $3.70 per pound")
        elif(userInput > 10):
            return("Package ", packNum, "will cost $3.80 per pound")

    def displayPrice(self):
        print(getPrice)

        

def main():
    #introduction
    userName = input("Hello, what is your name? \n")
    print("\nHello" , userName, "welcome to the Fast Freight Shipping Company self service.")
    
    x = 0
    userResponse = "y"
    packNum = 1
    
    while(x != 1):
        userInput = eval(input("Enter the weight of package: "))
        if(userInput >0):
            package = ShippingCharges(userInput)
            package.getPrice()
            package.displayPrice()
            packNum = packNum + 1
            userResponse = input("Would you like to send another package? y/n \n")
            if(userResponse == "n"):
                break
        elif(userInput <= 0):
            print("Package must be greater than 0")

    print("\nThank you", userName, "for our this services.")              


main()    

為什么在我的程序中明確定義了名稱“userInput”時,我卻一直收到它?

self.userInput userInput因為它是一個 class 變量

暫無
暫無

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

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