SORRY ДЛЯ неаккуратно и неэффективный кодДанные, хранимые в неверном объекте
Позвольте мне начать с основными правилами для кода. Мешок хранится под матрасом, и в каждый мешок могут входить только монеты. Индивидуально они могут содержать только один тип монет. Таким образом, сумочка не может содержать ни копейки, ни монету, только пенни.
Итак, проблема у меня здесь заключается в том, что когда я ввожу деньги, неважно, в какую «сумку» я закладываю деньги, монеты автоматически идут в сумку 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()
См. Также [Отладка небольших программ] (http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) –
Итак, я следил за тем, как вы его нашли и после еще несколько минут возиться, я смог увидеть, что происходит. Спасибо за совет –