class budgetapp():
def __init__(self,Deposit_amtt, Food, Cloth, Entertainment):
self.amtt = Deposit_amtt
self.Food = Food
self.Cloth = Cloth
self.Entertainment = Entertainment
def withdrawal(self):
for i in range (0,1):
print('Press 1 for Food\n')
print('Press 2 for Cloth\n')
print('Press 3 for Entertainment\n')
choice = int(input('Which Budget Category do you want to withdraw From?: '))
if choice == 1:
amt_withdrawal = float(input("How Much do you want to withdraw: "))
if amt_withdrawal>=0 and amt_withdrawal<= self.Food:
print("Withdrawal Successful")
elif amt_withdrawal > self.Food:
print("Insufficient Balance")
else:
print("Negative values cannot be withdrawn")
elif choice == 2:
amt_withdrawal = float(input("How Much do you want to withdraw: "))
if amt_withdrawal>=0 and amt_withdrawal<= self.Cloth:
print("Withdrawal Successful")
elif amt_withdrawal > self.Cloth:
print("Insufficient Balance")
else:
print("Negative values cannot be withdrawn")
elif choice == 3:
amt_withdrawal = float(input("How Much do you want to withdraw: "))
if amt_withdrawal>=0 and amt_withdrawal<= self.Entertainment:
print("Withdrawal Successful")
elif amt_withdrawal > self.Entertainment:
print("Insufficient Balance")
else:
print("Negative values cannot be withdrawn")
else:
print('Invalid choice. Options are between 1-3 only')
budg = budgetapp(float(input("Enter an amount to deposit: ")),
float(input("What percentage of total amount do you want to allocate to food: ")),
float(input("What percentage of total amount do you want to allocate to cloth: ")),
float(input("What percentage of total amount do you want to allocate to Entertainment: ")))
budg.withdrawal()发布于 2022-07-26 13:35:53
关于代码的提示:
码
class BudgetApp:
def __init__(self,deposit_amt, food, cloth, entertainment):
self.balance = deposit_amt
if food + cloth + entertainment == 100:
self.food = food * deposit_amt / 100.
self.cloth = cloth * deposit_amt / 100.
self.entertainment = entertainment * deposit_amt / 100.
else:
raise Exception("food, cloth, entertainment percent should sum to 100")
def withdrawal(self):
choice = int(input(f'''
Which Budget Category do you want to withdraw from?:
Please Enter:
1 for Food (balance: {self.food:.2f})
2 for Cloth (balance: {self.cloth:.2f})
3 for Entertainment (balance: {self.entertainment:.2f})'''))
if choice == 1:
amt_withdrawal = float(input("How Much do you want to withdraw: "))
if 0 < amt_withdrawal <= self.food:
self.balance -= amt_withdrawal
self.food -= amt_withdrawal
print("Withdrawal Successful")
elif amt_withdrawal > self.food:
print("Insufficient Food Balance")
else:
print("Negative values cannot be withdrawn")
elif choice == 2:
amt_withdrawal = float(input("How Much do you want to withdraw: "))
if 0 < amt_withdrawal <= self.cloth:
self.balance -= amt_withdrawal
self.cloth -= amt_withdrawal
print("Withdrawal Successful")
elif amt_withdrawal > self.cloth:
print("Insufficient Cloth Balance")
else:
print("Negative values cannot be withdrawn")
elif choice == 3:
amt_withdrawal = float(input("How Much do you want to withdraw: "))
if 0 < amt_withdrawal <= self.entertainment:
self.balance -= amt_withdrawal
self.entertainment -= amt_withdrawal
print("Withdrawal Successful")
elif amt_withdrawal > self.Entertainment:
print("Insufficient Entertainment Balance")
else:
print("Negative values cannot be withdrawn")
else:
print('Invalid choice. Options are between 1-3 only')
budg = BudgetApp(float(input("Enter an amount to deposit: ")),
float(input("What percentage of total amount do you want to allocate to food: ")),
float(input("What percentage of total amount do you want to allocate to cloth: ")),
float(input("What percentage of total amount do you want to allocate to Entertainment: ")))
budg.withdrawal()测试运行
Enter an amount to deposit: 100
What percentage of total amount do you want to allocate to food: 40
What percentage of total amount do you want to allocate to cloth: 30
What percentage of total amount do you want to allocate to Entertainment: 30
Which Budget Category do you want to withdraw from?:
Please Enter:
1 for Food (balance: 40.00)
2 for Cloth (balance: 30.00)
3 for Entertainment (balance: 30.00)1
How Much do you want to withdraw: 25
Withdrawal Successfulhttps://stackoverflow.com/questions/73122224
复制相似问题