python中的双重调用

2024-09-26 22:52:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我做这个程序是为了计算一个假设商店里商品的总价格,最后我遇到了一个双重呼叫。避免双重呼叫的最佳方法是什么

 #!/usr/bin/env python3
#Lukas Robin
#07.06.2021
class Stonk:
    def __init__(self, name ='', price = '', quantity = '', total = 0, shirtTotal = 0, sockTotal = 0, jeanTotal = 0):
        self.self = self
        self.name = name
        self.price = price
        self.quantity = quantity
        self.total = total
        self.shirtTotal = shirtTotal
        self.sockTotal = sockTotal
        self.jeanTotal = jeanTotal
    def main(__init__):
        __init__.name = ""
        __init__.total = 0
        __init__.shirtTotal = 0
        __init__.sockTotal = 0
        __init__.jeanTotal = 0
        __init__.price = {'shirts' : 25, 'socks': 12, 'jeans' : 50}
        __init__.name = input("Are you buying shirts, socks and/or jeans? ")
    def shirts(main, __init__):
        __init__.quantity = int(input("How many shirts would you like? "))
        __init__.shirtTotal = __init__.shirtTotal + (__init__.quantity*__init__.price['shirts'])
        print("The price of the shirts is, "+ str(__init__.shirtTotal))
        return __init__.shirtTotal
    def socks(main, __init__):
        __init__.quantity = int(input("How many socks would you like? "))
        __init__.sockTotal = __init__.sockTotal + (__init__.quantity*__init__.price['socks'])
        print("The price of the socks is, "+ str(__init__.sockTotal))
        return __init__.sockTotal
    def jeans(main, __init__):
        __init__.quantity = int(input("How many jeans would you like? "))
        __init__.jeanTotal = __init__.jeanTotal + (__init__.quantity*__init__.price['jeans'])
        print("The price of the jeans is, "+ str(__init__.jeanTotal))
        return __init__.jeanTotal
    main(__init__)
    options = __init__.name.split(", ")
    shirtTotals = shirts(main, __init__)
    sockTotals = socks(main, __init__)
    jeanTotals = jeans(main, __init__)
    for items in options:
        if items == ('socks' or 'sock'):
            socks(main, __init__)
        elif items == ('shirts' or 'shirt'):
            shirts(main, __init__)
        elif items == ('jeans' or 'jean'):
            jeans(main, __init__)
        __init__.total = shirtTotals+sockTotals+jeanTotals
        print("Your total is "+str(__init__.total))

(问题已经回答,感谢所有帮助过你的人。)


Tags: nameselfinputinitmaindefpricequantity
1条回答
网友
1楼 · 发布于 2024-09-26 22:52:51

你的问题是:

options = __init__.name.split(", ")
shirtTotals = shirts(main, __init__)
sockTotals = socks(main, __init__)
jeanTotals = jeans(main, __init__)

不管input("Are you buying shirts, socks and/or jeans? ")的结果如何,都会调用shirts()socks()jeans()

因此,我要做的第一个修复是删除shirtssocksjeans的重复项

因此,代码的底部如下所示:

main(__init__)
options = __init__.name.split(", ")
for items in options:
    if items == ('socks' or 'sock'):
        socks(main, __init__)
    elif items == ('shirts' or 'shirt'):
        shirts(main, __init__)
    elif items == ('jeans' or 'jean'):
        jeans(main, __init__)
    __init__.total = shirtTotals+sockTotals+jeanTotals

print("Your total is "+str(__init__.total))

即使有了这些修复,仍然存在对dunder方法的滥用

Dunder方法

Dunder方法或双下划线方法是一种特殊或神奇的方法,永远不会被直接调用。这方面的一个例子是__init__方法

如果我们要创建一个新的Stonk对象,我们不直接调用__init__,而是调用Stonk(),那么python将在后台调用__init__。这使代码更干净,因为我们没有在代码中散播init函数调用,而是看到了更清晰的Stonk()

因此,我们必须将__init__从它不属于的每个地方移除

相反,我们应该用引用当前对象的self替换它

通过这些更改,您的代码将如下所示:

class Stonk:

    def __init__(self):
        self.name = ""
        self.total = 0
        self.shirtTotal = 0
        self.sockTotal = 0
        self.jeanTotal = 0
        self.price = {'shirts': 25, 'socks': 12, 'jeans': 50}
        self.name = input("Are you buying shirts, socks and/or jeans? ")

    def shirts(self):
        quantity = int(input("How many shirts would you like? "))
        self.shirtTotal = self.shirtTotal + (quantity * self.price['shirts'])
        print("The price of the shirts is, "+ str(self.shirtTotal))
        return self.shirtTotal

    def socks(self):
        quantity = int(input("How many socks would you like? "))
        self.sockTotal = self.sockTotal + (quantity * self.price['socks'])
        print("The price of the socks is, " + str(self.sockTotal))
        return self.sockTotal

    def jeans(self):
        quantity = int(input("How many jeans would you like? "))
        self.jeanTotal = self.jeanTotal + (quantity * self.price['jeans'])
        print("The price of the jeans is, " + str(self.jeanTotal))
        return self.jeanTotal


stonk = Stonk() # creates new Stonk object

options = stonk.name.split(", ")

for items in options:
    if items == ('socks' or 'sock'):
        stonk.socks()
    elif items == ('shirts' or 'shirt'):
        stonk.shirts()
    elif items == ('jeans' or 'jean'):
        stonk.jeans()
    stonk.total = stonk.shirtTotals + stonk.sockTotals + stonk.jeanTotals
    print("Your total is "+str(stonk.total))

但我们有一个例外AttributeError: 'Stonk' object has no attribute 'shirtTotals'

如果我们比较以下代码块,我们将:

def socks(self):
    quantity = int(input("How many socks would you like? "))
    self.sockTotal = self.sockTotal + (quantity * self.price['socks'])
    print("The price of the socks is, " + str(self.sockTotal))
    return self.sockTotal

以及:

for items in options:
    if items == ('socks' or 'sock'):
        stonk.socks()
    elif items == ('shirts' or 'shirt'):
        stonk.shirts()
    elif items == ('jeans' or 'jean'):
        stonk.jeans()
    stonk.total = stonk.shirtTotals + stonk.sockTotals + stonk.jeanTotals

我们可以看到shirtTotalsshirtTotal不匹配,对于sockTotalsjeanTotals也存在同样的问题。这里的修正是将总计更改为总计

我们可以做的另一个改进是从构造函数中取出读取输入。这是因为在构造函数中读取输入是不直观的。通过这两项改进,您的代码将如下所示:

class Stonk:

    def __init__(self):
        self.name = ""
        self.total = 0
        self.shirtTotal = 0
        self.sockTotal = 0
        self.jeanTotal = 0
        self.price = {'shirts': 25, 'socks': 12, 'jeans': 50}

    def shirts(self):
        quantity = int(input("How many shirts would you like? "))
        self.shirtTotal = self.shirtTotal + (quantity * self.price['shirts'])
        print("The price of the shirts is, "+ str(self.shirtTotal))
        return self.shirtTotal

    def socks(self):
        quantity = int(input("How many socks would you like? "))
        self.sockTotal = self.sockTotal + (quantity * self.price['socks'])
        print("The price of the socks is, " + str(self.sockTotal))
        return self.sockTotal

    def jeans(self):
        quantity = int(input("How many jeans would you like? "))
        self.jeanTotal = self.jeanTotal + (quantity * self.price['jeans'])
        print("The price of the jeans is, " + str(self.jeanTotal))
        return self.jeanTotal

stonk = Stonk() # creates new Stonk object

options = input("Are you buying shirts, socks and/or jeans? ").split(", ")

for items in options:
    if items == ('socks' or 'sock'):
        stonk.socks()
    elif items == ('shirts' or 'shirt'):
        stonk.shirts()
    elif items == ('jeans' or 'jean'):
        stonk.jeans()

stonk.total = stonk.shirtTotal + stonk.sockTotal + stonk.jeanTotal
print("Your total is "+str(stonk.total))

用这些小补丁we can see your code running here

<强>我将考虑的编辑列表:

List of Edits I would consider making

相关问题 更多 >

    热门问题