Python 为什么赢了';这些类属性不是成倍增加吗?
我必须提示用户输入2项。对于每个项目,程序需要知道名称、价格和数量。一旦收到,它就会输出Python 为什么赢了';这些类属性不是成倍增加吗?,python,Python,我必须提示用户输入2项。对于每个项目,程序需要知道名称、价格和数量。一旦收到,它就会输出 '{Item name} {Item quantity} @ ${Item price} = {Item total}' 项目总数取价格并乘以数量。然而,我看不到总产量?” class Item: def __init__(self, name = 'none', price = 0, quantity = 0, total = 0): self.item_name = name
'{Item name} {Item quantity} @ ${Item price} = {Item total}'
项目总数取价格并乘以数量。然而,我看不到总产量?”
class Item:
def __init__(self, name = 'none', price = 0, quantity = 0, total = 0):
self.item_name = name
self.item_price = price
self.item_quantity = quantity
self.total = price * quantity
def __add__(self, other):
return self.total + other.total
def print_item_cost(self):
return print('{} {} @ ${} = ${}'.format(self.item_name,
self.item_price,
self.item_quantity,
self.total))
#Grab first item
item_1 = Item()
print('Item 1\nEnter the item name:')
item_1.item_name = input()
print('Enter the item price:')
item_1.item_price = input()
print('Enter the item quanity:')
item_1.item_quantity = input()
#Grab second item
item_2 = Item()
print('\nItem 2\nEnter the item name:')
item_2.item_name = input()
print('Enter the item price:')
item_2.item_price = input()
print('Enter the item quanity:')
item_2.item_quantity = input()
#Output cost
print('TOTAL COST\n', item_1.print_item_cost(), item_2.print_item_cost())
在下面的函数中,您要做的是返回语句的打印。您需要做的只是返回语句,并且发出调用的print语句将打印返回的输出。从此改变
def打印项目成本(自身):
返回打印('{}{}@${}=${}'。格式(self.item_name,
自项目价格,
self.item_数量,
自我评价(总计)
对此
def打印项目成本(自身):
返回({}{}@${}=${})。格式(self.item_name,
自项目价格,
self.item_数量,
自我评价(总计)
编辑:
您的总值不会更改,并且在初始化后保持不变,因此请添加一个新的calculate total方法
def计算_总计(自身):
self.total=self.price*self.quantity
并为每个对象调用此方法
calculate_total()
,以重新计算总数您正在使用空参数列表创建项目:
item_1 = Item()
因此,默认值用于\uuuuu init\uuuuu
:价格=0,数量=0
,因此self.total
计算为0。稍后,您将更改现有对象的price
和quantity
属性:
item_2.item_price = input()
item_2.item_quantity = input()
但这不会改变总数。您可能应该做的是:
#Grab first item
print('Item 1')
name = input('Enter the item name:')
price = input('Enter the item price:')
quantity = input('Enter the item quanity:')
item_1 = Item(name, price, quantity)
(与
项目2
相同)有几件事,您在初始化期间设置了总价格,因此您必须在初始价格处设置项目价格
和项目数量
。另一件事是,当您接受来自用户的数字输入时,您必须将其解析为适当的数据类型,因为默认情况下它是字符串
这将按照您的预期工作
class Item:
def __init__(self, name = 'none', price = 0, quantity = 0, total = 0):
self.item_name = name
self.item_price = price
self.item_quantity = quantity
self.total = price * quantity
def __add__(self, other):
return self.total + other.total
def print_item_cost(self):
return ('{} {} @ ${} = ${}'.format(self.item_name,
self.item_price,
self.item_quantity,
self.total))
#Grab first item
print('Item 1\nEnter the item name:')
item_name = input()
print('Enter the item price:')
item_price = int(input())
print('Enter the item quanity:')
item_quantity = int(input())
item_1 = Item(item_name, item_price, item_quantity)
#Grab second item
print('\nItem 2\nEnter the item name:')
item_name = input()
print('Enter the item price:')
item_price = int(input())
print('Enter the item quanity:')
item_quantity = int(input())
item_2 = Item(item_name, item_price, item_quantity)
#Output cost
print('TOTAL COST\n', item_1.print_item_cost(), item_2.print_item_cost())
- 必须在提供实际值后计算总数
input()
返回python 3中的字符串,应转换为整数(或浮点)
更改打印项目成本
功能的名称,并从该功能中删除打印
;打印或返回字符串,但不能同时打印或返回字符串
类项目:
定义初始化(self,名称='none',价格=0,数量=0,总数=0):
self.item_name=名称
self.item_price=价格
self.item\u数量=数量
#self.total=价格*数量
def总计(自身):
返回自项目价格*自项目数量
定义添加(自身、其他):
返回self.total+other.total
def项目成本字符串(自身):
返回'{}{}@${}=${}'。格式(self.item_name,
自项目价格,
self.item_数量,
self.total())
#抢先
第1项=第()项
打印('项目1\n输入项目名称:')
item_1.item_name=输入()
打印('输入项目价格:')
item_1.item_price=int(输入())
打印('输入项目数量:')
item_1.item_quantity=int(输入())
#抓取第二项
项目2=项目()
打印('\nItem 2\n输入项目名称:')
item_2.item_name=输入()
打印('输入项目价格:')
item_2.item_price=int(输入())
打印('输入项目数量:')
item_2.item_quantity=int(输入())
#产出成本
打印('TOTAL COST\n',item_1.item_COST_string(),'\n',item_2.item_COST_string())
当您执行以下操作时
self.total = price * quantity
执行此行时,它使用price
和quantity
的当前值来设置self.total
的值。当价格
或数量
的值发生变化时,不定义一个公式,当价格
或数量
的值发生变化时,该公式将自动更新self.total
。理想情况下,在类中需要一个方法,该方法在接收所有输入后设置总计的值。请阅读调试代码的提示。这将帮助您理解您认为代码正在做的事情和它实际做的事情之间的差异。