数据存储在错误的对象

时间:2015-09-29 00:34:45

标签: python

对于有效和无效的代码抱歉

让我先从代码的基本规则开始。袋子存放在床垫下面,每个袋子只能包含硬币。单独地,它们只能包含一种类型的硬币。因此,便士袋不能包含硬币或任何其他硬币,只有便士。

好吧,我在这里的问题是,当我存钱时,无论我存入什么“袋”,硬币自动转到Dime包,我找不到代码中的哪个位置发生。从我的观点来看,代码是有意义的(它对我而言,但不是计算机)。我真的觉得我只需要第二眼看着它。我也不是一个没有真正帮助的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()

1 个答案:

答案 0 :(得分:0)

我找到了你的问题。

让我向您解释我是如何找到您的问题的,所以下次您可以找到它。

我在代码中添加了一行

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

考虑到你的程序尝试做什么,我希望在我尝试添加季度时打印。但它从未打印过,表明在此之前已经发生了一些事情。

接下来我添加了另一行:

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

然后我再次运行程序,然后打印出来。

现在我知道问题出在两个打印语句之间。鉴于该程序最终将其添加到角钱中,它使得它看起来像某种程度上我们最终运行了角钱添加代码,即使我输入了q。我查看检查进入角钱部分的代码并查看问题。

我看到了问题,但是为了进行教学练习,我认为你应该自己找到它。