【发布时间】:2021-06-13 04:31:05
【问题描述】:
我正在编写这个程序来计算假设商店中商品的总价格,最后我遇到了双重电话。避免这种双重调用的最佳方法是什么?
#!/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))
(问题已解答,感谢所有帮助过的人。)
【问题讨论】:
-
您是否有意尝试编写难以理解的代码?你到底为什么要使用
__init__和main作为方法的第一个参数的名称?? -
无论如何,这是否准确地反映了您的缩进?从您的描述中,您的问题完全不清楚。请参阅How to Ask 和help center
-
对不起,如果程序难以理解,我是python的初学者。问题出在第 43-50 行的某个地方,函数被调用了两次,我想知道是否有任何方法可以解决这个问题?
-
这不是一个充分的问题规范。 再次请参阅我上面链接的资源,以帮助编写更清晰的问题
-
@RustyB 我建议不要使用 w3schools。不是很好的资源。最好去official tutorial/documentation
标签: python python-3.x oop