#這是我的代碼,將添加借出和撤回,但似乎我的代碼是錯誤的,但它沒有
class Money:
loaned = 0
withdrawed = 0
totalMoney = 0
def bankMoney (self):
self.totalMoney = self.withdrawed self.loaned
return totalMoney
if totalMoney >= 1000000:
print(" enough money,")
else:
print("not enough")
m1 = Money()
m1.loaned = float(input("loaned"))
m1.withdrawed = float(input("withdrawed"))
#then 當我嘗試執行它時。它只是詢問用戶但沒有解決
uj5u.com熱心網友回復:
- 如果要在方法中使用 print 陳述句,請洗掉 return 陳述句
bankMoney,因為 return 之后的陳述句不會執行。 self.在totalMoneyif 陳述句處添加- 呼叫方法
m1.bankMoney來執行它
嘗試以下操作: 代碼
class Money:
loaned = 0
withdrawed = 0
totalMoney = 0
def bankMoney (self):
self.totalMoney = self.withdrawed self.loaned
if self.totalMoney >= 1000000:
print(" enough money,")
else:
print("not enough")
m1 = Money()
m1.loaned = float(input("loaned "))
m1.withdrawed = float(input("withdrawed "))
m1.bankMoney()
輸出
loaned 1000000
withdrawed 1
enough money,
uj5u.com熱心網友回復:
存在三個問題:
- 該功能未執行
- 您的 return 陳述句沒有正確縮進
- 你不能在列印之前回傳,那么你將根本無法執行列印陳述句
試試吧:
class Money:
def __init__(self):
self.totalMoney = 1000000
self.loaned = float(input("loaned: "))
self.withdrawed = float(input("withdrawed: "))
def bankMoney (self):
total = self.withdrawed self.loaned
if total >= self.totalMoney:
print(" enough money")
else:
print("not enough")
return total
m1 = Money()
m1.bankMoney()
uj5u.com熱心網友回復:
像這樣:
class Money():
def __init__(self,l, w):
self.loaned = l
self.withdrawed = w
self.totalMoney = 0
def bankMoney(self):
self.totalMoney = self.withdrawed self.loaned
if self.totalMoney >= 1000000:
print("enough money,")
else:
print("not enough")
return self.totalMoney # this is returned at the end
loaned = float(input("loaned : "))
withdrawed = float(input("withdrawed: "))
m1 = Money(loaned, withdrawed)
print(m1.bankMoney()) # the method within the class is to be called.
輸出:
loaned : 555.4444
withdrawed: 654654653545.89
enough money,
654654654101.3345
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/430946.html
