简体   繁体   English

当我在 Python 中使用 __repr___ 时,我的代码没有打印客户的列表

[英]My code is not printing the customer's list when I use __repr___ in Python

I don't want to confuse anyone with this question but I will try my best to write it.我不想让任何人对这个问题感到困惑,但我会尽力写出来。

As from my previous questions, I am writing a text adventure game where a customers order a pizza and you make it for them.正如我之前的问题一样,我正在编写一个文字冒险游戏,其中客户订购比萨饼,您为他们制作。 If you get all of the toppings correct the customer will be satisfied and you go to the next level.如果你把所有的浇头都弄对了,客户会很满意,你 go 会更上一层楼。

I was messing around with the repr to change how the customer's customerdir prints instead of it showing brackets.我在搞乱repr以更改客户的customerdir打印方式,而不是显示括号。 The customerdir is a list of toppings that the customer wants on the pizza. customerdir是客户想要在比萨上添加的配料列表。 When the player makes the pizza the toppings go into the player's last.pizza list.当玩家制作披萨时,配料 go 进入玩家的last.pizza列表。 If the last.pizza list is equal to the customerdir then the player made the order right and goes on to the next level.如果last.pizza列表等于customerdir ,则玩家正确下单并进入下一个级别。

Everything was going great but now when I execute the code every customer wants Pineapples and Ham.一切都很顺利,但现在当我执行代码时,每个客户都想要菠萝和火腿。 Which is wrong because every customer has their own customerdir list with the topping that they want.这是错误的,因为每个客户都有自己的customerdir列表,其中包含他们想要的顶部。

See:看:

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())

Heres the Customer Class:这是客户 Class:

    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.'

Here is part of the making_pizza method where the boss tells me that a customer wants a pizza.这是making_pizza 方法的一部分,老板告诉我客户想要披萨。 As you can see I used str(self) instead of str(self.customerdir) so that the print function can print what I put in the repr .如您所见,我使用 str(self) 而不是 str(self.customerdir) 以便 print function 可以打印我放入repr的内容。 I tried (self.customerdir) but it just prints out None .我试过 (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.") 

So to sum it up.所以总结一下。 When I execute the code it now prints out Joe: He would like a Pineapple and Ham Pizza.当我执行代码时,它现在打印出Joe:他想要一个菠萝和火腿披萨。 The customer does not want the pizza to be sliced.顾客不希望披萨被切片。 for each.对于每个。 customer even though every customer don't have Pineapple and Ham in their customerdir list.即使每个客户的customerdir列表中都没有 Pineapple 和 Ham。 It's not printing out the other text that I put in the repr .它没有打印出我放入repr的其他文本。 Here is the full code on pastebin - https://pastebin.com/9ePT8Rj2这是 pastebin 的完整代码 - https://pastebin.com/9ePT8Rj2

Thank you so much guys and sorry for the lost question and sorry if this is a bad question to ask.非常感谢你们,对丢失的问题感到抱歉,如果这是一个不好的问题,我很抱歉。

As indicated in the comments, the problem is that you use str(self.customerdir ==...) .如评论中所示,问题在于您使用str(self.customerdir ==...) This will result in the answer 'True' , or 'False' (note the string quotes), which will evaluate to True when you call bool('True') and bool('False') .这将导致答案'True''False' (注意字符串引号),当您调用bool('True')bool('False')时,其评估结果为 True。

Also there are a lot of different representations that you could make for a Pizza.此外,您可以为比萨饼制作许多不同的表示形式。 Which is similar to your customer class.这类似于您的客户 class。 So a good abstraction would be to create a Pizza class per customer and build the representation logic into that class.所以一个好的抽象是为每个客户创建一个 Pizza class 并将表示逻辑构建到 class 中。 Also please note the difference between __str__ and __repr__ , called by str() and repr() respectively.另请注意__str____repr__之间的区别,分别由str()repr()调用。

A Pizza class that could always represent your current order would look something like:始终可以代表您当前订单的 Pizza class 看起来像:

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

Which can be used as:可以用作:

>>> 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.

And for comparison:为了比较:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM