我是python的新手,我仍然试图掌握它。我正在尝试更改以下代码中的处理功能,以便用户无法提取更多的钱,而“银行”记录的内容是500.我希望有人可以提供帮助。我会为> 500输入if语句吗?
#Simple Bank Atm
def main():
PIN=7777;balance=500;pin=0;success=False
Pin=getInput(pin)
Pin,PIN,balance,success=processing(pin,PIN,balance,success)
Display(success,balance)
#Input Function
def getInput(pin):
pin=int(input(“Please enter your PIN:”))
return pin
#Processing Function
def processing(pin,PIN,balance,success):
if pin==PIN:
success=True
amt=float(input(“How much would you like to withdraw?”))
balance=balance-amt
return pin,PIN,balance,success
else:
success=false
return pin,PIN,balance,success
答案 0 :(得分:0)
您可以使用if
条件来执行此操作。
if amt <= balance: #Can't withdraw over your current balance
balance -= amt
else:
print("Error. Amount exceeds funds on record.")
另外,除了你之外的其他事情,你会在if
和else
条件中返回相同的内容,如果这是你真正想要归还的东西,那么它是多余的同时。您可以在同一缩进级别的else语句之后获得它
答案 1 :(得分:0)
def main():
PIN=7777;balance=500;pin=0;success=False
Pin=getInput(pin)
Pin,PIN,balance,success=processing(pin,PIN,balance,success)
Display(success,balance)
#Input Function
def getInput(pin):
pin=int(input("Please enter your PIN:"))
return pin
#Processing Function
def processing(pin,PIN,balance,success):
if pin==PIN:
success=True
amt=float(input("How much would you like to withdraw?"))
if balance<amt:
print("Amount to draw is greater than balance.")
return pin,PIN,balance,false
balance=balance-amt
return pin,PIN,balance,success
else:
success=false
return pin,PIN,balance,success
答案 2 :(得分:0)
是。你应该使用if语句 - 这应该在它们可以提取金额之前发生(余额=余额 - amt)。
所以你可以这样做:
if amt <= balance:
balance -= amt
return True
else:
# do not change balance and stop withdrawal
return False