查看注册Python项目

问题描述 投票:0回答:1

我正在使用 PyCharm 对超市收银台进行 Uni 评估:

我已经创建了“Product”和“CheckoutRegister”类,它们工作正常,但由于某种原因我的主模块代码无法工作。

这些是课程:

class Product:
    def __init__(self, barcode, name, price):
        self.barcode = barcode
        self.name = name
        self.price = price

    def get_barcode(self):
        return self.barcode

    def get_name(self):
        return self.name

    def get_price(self):
        return self.price


class CheckoutRegister:
    def __init__(self):
        self.__total_payment = 0.0
        self.__received_amount = 0.0
        self.__purchased_products = []

    def get_purchased_products(self):
        return self.__purchased_products

    # Scanning products
    def scan_product(self, some_product):
        self.__total_payment += some_product.get_price()
        self.__purchased_products.append(some_product)

    def get_total_payment(self):
        return self.__total_payment

    # Accepting payment
    def accept_payment(self, some_amount):
        self.__received_amount += some_amount
        return self.__received_amount

    # Printing receipt
    def print_receipt(self):
        message = "\n********** Final Receipt *********\n\n"
        for p in self.__purchased_products:
            message += p.get_name() + " $" + str(p.get_price()) + "\n"
        message += "\nTotal Amount due : $" + str(self.__total_payment) + "\n"
        message += "Amount Received : $" + str(self.__received_amount) + "\n"
        message += "Balance given : $" + str(self.__received_amount - self.__total_payment) + "\n"
        message += "Thanks for shopping at AJ Groceries Store!\n"
        return message

    def save_transaction(self, current_date, barcode, price):
        message = str(current_date) + " " + str(barcode) + " " + str(price) + "\n"
        return message

主模块代码:


# Import Classes
from CheckoutRegister import CheckoutRegister
from Product import Product
from datetime import datetime


def main():
    product_list = []
    with open("products.txt", "rt") as file:
        for each_line in file:
            name, price, barcode = each_line.strip().split(",")
            product_list.append(Product(name, float(price), int(barcode)))

    while True:
        # Coding the program
        print()
        print("***** Welcome to AJ Groceries Store! *****")
        print()

        checkout = CheckoutRegister()
        # Loop for scanning product
        while True:
            code = int(input("Please enter the product Barcode: "))
            found = False

            for product in product_list:
                if product.get_barcode() == code:
                    found = True
                    selected_product = product
                    break
            if found:
                print("\n{} - ${}\n".format(selected_product.get_name(), selected_product.get_price()))
                checkout.scan_product(selected_product)
            else:
                print("\nERROR!! – Scanned barcode is incorrect.\n")

            ans = input("Would you like to scan another product? (Y/N)").upper()
            print()
            if ans == "N":
                break
            elif ans != "Y" and ans != "N":
                print("\n*** Wrong input ***\n")

        amount_due = checkout.get_total_payment()

        # Loop for accepting payment
        while True:
            amount = float(input("Payment due: ${}. Please enter an amount to pay: ".format(amount_due)))
            if amount < 0:
                print("We do not accept negative amounts.")
                continue

            checkout.accept_payment(float(amount))
            amount_due -= float(amount)
            if amount_due <= 0:
                break

        # Print receipt
        print(checkout.print_receipt())

        # Call saved transaction
        current_date = datetime.today()
        with open("transactions.txt", "a") as file:
            for product in checkout.get_purchased_products():
                line = checkout.save_transaction(current_date, product.get_barcode(), product.get_price())
                file.write(line + "\n")

        next_customer = input("(N)ext customer, or (Q)uit? ").upper()
        print()

        if next_customer == "Q":
            break
    main()

有人可以向我解释一下我做错了什么吗?

python pycharm
1个回答
0
投票

你的主循环处于无限递归中。这是避免无限递归的修改:

def main():
    # Your existing code here

if __name__ == "__main__":
    main()

main()
函数在其自身定义结束时被递归调用。如果用户决定继续与下一个客户联系,这可能会造成无限循环。您不应再次调用
main()
,而应使用循环来询问是否处理新客户,并且仅在用户决定退出时才退出程序。您已经与 (N)ext customer 或 (Q)uit 有此循环?,因此您应该删除对
main()
的递归调用。

© www.soinside.com 2019 - 2024. All rights reserved.