数据存储在错误的对象中

2024-04-25 00:41:56 发布

您现在位置:Python中文网/ 问答频道 /正文

很抱歉,代码混乱且效率低下

让我从代码的基本规则开始。一个袋子放在床垫下,每个袋子只能装硬币。它们各自只能包含一种硬币。所以硬币袋里不能装一角硬币或其他硬币,只能装硬币。你知道吗

好吧,我这里的问题是,当我存钱的时候,不管我存到什么“袋子”里,硬币会自动转到一角硬币袋子里,我在代码里找不到发生这种情况的地方。从我的观点来看,代码是有意义的(对我来说是有意义的,但对计算机来说不是)。我真的觉得我需要第二双眼睛看看。我也不是一个python程序员,这对我没什么帮助

import os
class Bags:
    def __init__(self, nOC, n, v):
        self.name = n
        self.numOfCoins = nOC
        self.value = v
        self.SetBal()

    # Setters
    def SetBal(self):
        self.amount = self.numOfCoins * self.value

    def SetCoin(self, coin):
        self.numOfCoins += coin
        self.SetBal()

    # Getters
    def GetBal(self):
        return self.amount

class Mattress:
    def __init__(self):
        self.pBag = Bags(0, "Pennies", 0.01)
        self.dBag = Bags(0, "Dimes", 0.05)
        self.nBag = Bags(0, "Nickles", 0.10)
        self.qBag = Bags(0, "Quarters", 0.25)
        self.boBag = Bags(0, "Bug-Out-Bag", 0.00)


    # Setters


    # Getters
    def GetBalances(self):
        pen = self.pBag.GetBal()
        print("Balance of pennies : $%.2f. %d pennies" % (pen,     self.pBag.numOfCoins))
        print("Balance of dimes   : $%.2f. %d dimes" % (self.dBag.GetBal(), self.dBag.numOfCoins))
        print("Balance of nickles : $%.2f" % self.nBag.GetBal())
        print("Balance of quarters: $%.2f" % self.qBag.GetBal())
        total = self.pBag.GetBal() + self.qBag.GetBal() + self.dBag.GetBal() + self.nBag.GetBal()
        print("Total              : $%.2f" % total)


def main ():
    # Check laod or what not for what you have to do and all that good stuff
    userMain = Mattress()
    mainLoop = True
    menu = '''What would you like to do?
    D. Deposit Money
    W. Withdraw Money
    T. Transfer Funds
    S. Show Balances
    E. Exit'''

    diffBags = '''
    P. Pennies
    D. Dimes
    N. Nickles
    Q. Quarters
    C. Cancel'''

    while(mainLoop):
        print(menu)

        action = input("Select an option: ")

        if action == 'D' or action == 'd' :
            depositMenu = "What bag would you like to deposit into? " + diffBags
            depLoop = True
            while(depLoop):
                print(depositMenu)

                depAction = input("Select an option: ")
                depAmt = "How much would you like to deposit? "
                if depAction == 'P' or action == 'p':
                    while True:
                        try:
                            depCoin = int(input(depAmt))
                            if depCoin < 1:
                                print("Invalid. Please enter a positive number")
                                continue
                            break
                        except ValueError:
                            print("Invalid. Please enter a positive number")

                    userMain.pBag.SetCoin(depCoin)
                    depLoop = False
                elif depAction == 'D' or action == 'd':
                    while True:
                        try:
                            depCoin = int(input(depAmt))
                            if depCoin < 1:
                                print("Invalid. Please enter a positive number")
                                continue
                            break
                        except ValueError:
                            print("Invalid. Please enter a positive number")

                    userMain.dBag.SetCoin(depCoin)
                    depLoop = False
                elif depAction == 'N' or action == 'n':
                    while True:
                        try:
                            depCoin = int(input(depAmt))
                            if depCoin < 1:
                                print("Invalid. Please enter a positive number")
                                continue
                            break
                        except ValueError:
                            print("Invalid. Please enter a positive number")

                    userMain.nBag.SetCoin(depCoin)
                    depLoop = False
                elif depAction == 'Q' or action == 'q':
                    while True:
                        try:
                            depCoin = int(input(depAmt))
                            if depCoin < 1:
                                print("Invalid. Please enter a positive number")
                                continue
                            break
                        except ValueError:
                            print("Invalid. Please enter a positive number")

                    userMain.qBag.SetCoin(depCoin)
                    depLoop = False
                elif depAction == 'C' or action == 'c':
                    depLoop = False

        elif action == 'W' or action == 'w':
            print ("working on it")


        elif action == 'T' or action == 't':
            print ("working on it")


        elif action == 'S' or action == action == 's':
            userMain.GetBalances()

        elif action == 'E' or action == 'e':
            print("Have a great day")
            mainLoop = False
        else:
            print ("Incorrect value")

if __name__ == "__main__":
    main()

Tags: orselfnumberifdefaction硬币print
1条回答
网友
1楼 · 发布于 2024-04-25 00:41:56

我发现了你的问题。你知道吗

让我向你解释一下我是如何发现你的问题的,这样下次你就可以找到它了。你知道吗

我给你的代码加了一行

print("Q")
userMain.qBag.SetCoin(depCoin)

考虑到你的程序试图做什么,我希望当我试图添加四分之一时,它会被打印出来。但它从来没有被打印出来,表明在那一点之前已经发生了什么。你知道吗

接下来我又加了一行:

depAction = input("Select an option: ")
print("GOT", depAction)

然后我再次运行程序,然后打印出来。你知道吗

现在我知道问题出在两个打印语句之间。考虑到程序最终将其添加到dimes中,这使它看起来像是我们在运行dimes时添加了代码,即使我输入了q。我查看了检查dimes部分的代码,发现了问题。你知道吗

我明白问题所在,但为了做一个说教练习,我认为你应该自己去发现它。你知道吗

相关问题 更多 >