overmind 2023-11-25 10:19 采纳率: 82.4%
浏览 11
已结题

程序使用如此的结构合理吗?如果不合理的话应该如何修改。

  • 总共分成4个py文件
    • 主控 main.py
    • 输入 get_fruits.py
    • 处理 process.py
    • 输出 output.py

--------以下是具体程序-------------

  • main.py
import get_fruits
import process
import output
  • get_fruits.py
try:
    a = input("How many apples do you got?\n")
    a = int(a)
    print("You got " + str(a) + " apples!")
    b = input("How many bananas do you got?\n")
    b = int(b)
    print("You got " + str(b) + " bananas!")
except ValueError:
    print("\33[41merror\33[0m",end="")
    print("your input is invalid!")
    quit()
  • process.py
try:
    from get_fruits import a,b
    total = a + b
except ValueError:
    print("\33[41merror\33[0m",end="")
    print("your input is invalid!")
    quit()
  • output.py
try:
    from process import total
    print("You got " + str(total) + " fruits!")
except ValueError:
    print("\33[41merror\33[0m",end="")
    print("your input is invalid!")
    quit()
  • 写回答

18条回答 默认 最新

  • IT·陈寒 优质创作者: 编程框架技术领域 2023-11-25 13:49
    关注

    你的程序结构在关注点分离方面是合理的,每个模块(文件)都有一个特定的责任。然而,有一些改进可以提高代码的组织性和可读性:

    1. 避免使用全局变量:

      • 避免使用像 abtotal 这样的全局变量,考虑在每个模块中定义函数,并将必要的值作为参数传递。
    2. 使用函数提高可重用性:

      • 将代码封装在函数中,接受参数并返回值。这使得代码更具模块性和可重用性。
    3. 适当处理错误:

      • 遇到错误时,不要使用 quit() 直接退出程序,考虑引发异常或返回错误代码。这允许调用代码决定如何处理错误。

    以下是如何修改你的代码的示例:

    main.py:

    import get_fruits
    import process
    import output
    
    def main():
        try:
            apples = get_fruits.get_apples()
            bananas = get_fruits.get_bananas()
            total = process.calculate_total(apples, bananas)
            output.print_result(total)
        except ValueError as e:
            print("\33[41merror\33[0m", end="")
            print(str(e))
    
    if __name__ == "__main__":
        main()
    

    get_fruits.py:

    def get_apples():
        try:
            apples = int(input("How many apples do you have?\n"))
            print("You have " + str(apples) + " apples!")
            return apples
        except ValueError:
            raise ValueError("Your input for apples is invalid!")
    
    def get_bananas():
        try:
            bananas = int(input("How many bananas do you have?\n"))
            print("You have " + str(bananas) + " bananas!")
            return bananas
        except ValueError:
            raise ValueError("Your input for bananas is invalid!")
    

    process.py:

    def calculate_total(apples, bananas):
        try:
            total = apples + bananas
            return total
        except ValueError:
            raise ValueError("Error calculating total!")
    

    output.py:

    def print_result(total):
        try:
            print("You have a total of " + str(total) + " fruits!")
        except ValueError:
            raise ValueError("Error printing result!")
    

    这种修改更符合模块化编程的原则,使得更容易管理和扩展你的代码。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(17条)

报告相同问题?

问题事件

  • 系统已结题 12月10日
  • 已采纳回答 12月2日
  • 创建了问题 11月25日