首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >这是我在python中的第一个项目。我希望确保预算类别(食品、布料和娱乐)的分配值不大于self.amtt

这是我在python中的第一个项目。我希望确保预算类别(食品、布料和娱乐)的分配值不大于self.amtt
EN

Stack Overflow用户
提问于 2022-07-26 11:04:21
回答 1查看 32关注 0票数 -1
代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

发布于 2022-07-26 13:35:53

关于代码的提示:

  • 使用Python样式指南命名约定
  • 构造函数(即方法init)
    • 食物、衣服和娱乐津贴是存款金额的百分比
    • 将这些从百分比换算为金额
    • 检查总数为100的百分比
    • 如果不例外,则引发异常。

  • 方法提取
    • 显示与每个选择选项的平衡
    • 检查足够的提款资金
    • 如果撤回成功,则更新余额

代码语言:javascript
复制
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()

测试运行

代码语言:javascript
复制
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 Successful
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73122224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档