首页 > 解决方案 > 当我在 Python 中使用 __repr___ 时,我的代码没有打印客户列表

问题描述

我不想让任何人对这个问题感到困惑,但我会尽力写出来。

正如我之前的问题一样,我正在编写一个文字冒险游戏,其中客户订购比萨饼,您为他们制作。如果你把所有的配料都弄对了,客户就会满意,你就会进入下一个层次。

我在搞乱repr以更改客户的customerdir打印方式,而不是显示括号。customerdir是客户想要在比萨上添加的配料列表。当玩家制作披萨时,配料会进入玩家的last.pizza列表。如果last.pizza列表等于customerdir,则玩家正确下单并进入下一个级别。

一切都很顺利,但现在当我执行代码时,每个客户都想要菠萝和火腿。这是错误的,因为每个客户都有自己的customerdir列表,其中包含他们想要的顶部。

看:

customer1 = Customer("Jerry", "43 Munan Road", ['Sausages', 'Pepperoni', 'Sliced'].sort())
customer2 = Customer('Mike', "302 Anerley Court", ['Cheese', 'Sliced'].sort())
customer3 = Customer("Lucas", "721 Golf Road", ['Pineapples', 'Ham', 'Not Sliced'].sort())
customer4 = Customer("Tom", "93 Posh Road", ['Sausages', 'Pepperoni', 'Sliced'].sort())
customer5 = Customer('Freddy', "131 Marlon Road", ['Cheese', 'Sliced'].sort())
customer6 = Customer("Arnold", "500 Beach Street", ['Pineapples', 'Ham', 'Not Sliced'].sort())
customer7 = Customer("Lisa", "22 Payton Circle", ['Sausages', 'Pepperoni', 'Sliced'].sort())
customer8 = Customer('Kelly', "Flat A Dustin Court", ['Cheese', 'Sliced'].sort())
customer9 = Customer("Analiese", "90 Simpson Road", ['Pineapples', 'Ham', 'Not Sliced'].sort())

这是客户类:

    class Customer:
        def __init__(self, name, address, customerdir):
            self.name = name
            self.address = address
            self.customerdir = customerdir
    
        def __repr__(self):
            if str(self.customerdir == ['Pineapples', 'Ham', 'Not Sliced']):
                return 'Pineapple and Ham Pizza. The customer does not want the pizza to be sliced.'
            elif str(self.customerdir == ['Sausages', 'Pepperoni', 'Sliced']):
                return 'Sausage and Pepperoni Pizza that is sliced.'
            elif str(self.customerdir == ['Cheese', 'Sliced']):
                return 'Cheese Pizza that is sliced.'

这是making_pizza 方法的一部分,老板告诉我客户想要披萨。如您所见,我使用 str(self) 而不是 str(self.customerdir) 以便 print 函数可以打印我放入repr的内容。我试过 (self.customerdir) 但它只是打印出None

 print("Joe:" + mPlayer.name + " We have a customer by the name of " + str(self.name))
            time.sleep(a)
            print("Joe: He would like a " + str(self))
            time.sleep(a)
            print("The address is " + self.address)
            time.sleep(a)
            accept = input('Do you accept? (Yes/No):')
            if accept == 'Yes' or accept == 'yes':
                pizza_menu()
            elif accept == 'No' or accept == 'no':
                print("Joe: Fine I'll find someone else for the job.") 

所以总结一下。当我执行代码时,它现在打印出Joe:他想要一个菠萝和火腿披萨。顾客不希望披萨被切片。对于每个。即使每个客户的customerdir列表中都没有 Pineapple 和 Ham。它没有打印出我放入repr的其他文本。这是 pastebin 的完整代码 - https://pastebin.com/9ePT8Rj2

非常感谢你们,对丢失的问题感到抱歉,如果这是一个不好的问题,我很抱歉。

标签: pythonpython-3.x

解决方案


如评论中所示,问题在于您使用str(self.customerdir == ...). 这将导致答案'True',或(注意字符串引号),当您调用and'False'时,它将评估为 True 。bool('True')bool('False')

此外,您可以为比萨饼制作许多不同的表示形式。这类似于您的客户类别。因此,一个好的抽象是为每个客户创建一个 Pizza 类,并将表示逻辑构建到该类中。另请注意 和 之间的区别,分别由__str____repr__调用。str()repr()

始终可以代表您当前订单的 Pizza 类看起来像:

class Pizza:
    def __init__(self, toppings, sliced):
        self.toppings: set = set(toppings)   
        self.sliced = sliced
    def __repr__(self):
        if self.sliced:
            return f"{' and '.join(self.toppings)} Pizza. The customer does not want the pizza to be sliced."
        return f"{' and '.join(self.toppings)} Pizza that is sliced." 
    def __eq__(self, other):
        if len(other.toppings - self.toppings or self.sliced is not other.sliced:
            return False
        return True

可以用作:

>>> Pizza(toppings=['Pineapple', 'Ham'], sliced=False)
Pineapple and Ham Pizza that is sliced.
>>> Pizza(toppings=['Pineapple', 'Ham'], sliced=True)
Pineapple and Ham Pizza. The customer does not want the pizza to be sliced.

为了比较:

>>> pizza_ham = Pizza(toppings=['Ham'], sliced=False)
>>> pizza_pepperoni = Pizza(toppings=['Pepperoni'], sliced=False)
>>> pizza_ham == pizza_pepperoni
False

推荐阅读