I'm running a simple online shopping cart program and when I tried to run it, the ending result was blank. I understand the concept about classes and objects, but I really need assistance. It is supposed to look like this:
Item 1
Enter the item name: Chocolate Chips
Enter the item price: 3
Enter the item quantity: 1
Item 2
Enter the item name: Bottled Water
Enter the item price: 1
Enter the item quantity: 10
TOTAL COST
Chocolate Chips 1 @ 3ドル = 3ドル
Bottled Water 10 @ 1ドル = 10ドル
Total: 13ドル
Here's what I've written so far, :
class ItemsToPurchase :
def __init__(self, item_name = "none", item_price = 0, item_quantity = 0):
self.item_name = item_name
self.item_price = item_price
self.item_quantity = item_quantity
def print_item_cost(self):
total = item_quantity * item_price
print('%s %d @ $%f = $%f' % (item_name, item_quantity, item_price, total))
def main():
print('Item 1')
print()
item_name = str(input('Enter the item name: '))
item_price = float(input('Enter the item price: '))
item_quantity = int(input('Enter the item quantity: '))
item_one = ItemsToPurchase(item_name, item_price, item_quantity)
item_one.print_item_cost()
print('Item 2')
print()
item_name = str(input('Enter the item name: '))
item_price = float(input('Enter the item price: '))
item_quantity = int(input('Enter the item quantity: '))
item_two = ItemsToPurchase(item_name, item_price, item_quantity)
item_two.print_item_cost()
print('TOTAL COST')
item_one.print_item_cost()
item_two.print_item_cost()
if __name__ == "__main__":
main()
What did I do wrong?
Jarvis
8,6023 gold badges33 silver badges61 bronze badges
-
Not sure if this is just in you asking the question but, the call to main() isn't indented under the if statementSighonide– Sighonide2016年11月30日 04:47:06 +00:00Commented Nov 30, 2016 at 4:47
-
What's the error you are getting?Prajwal– Prajwal2016年11月30日 04:48:32 +00:00Commented Nov 30, 2016 at 4:48
-
Is this being run in a separate process?Sighonide– Sighonide2016年11月30日 04:48:36 +00:00Commented Nov 30, 2016 at 4:48
-
The print_item_cost method has a problem. you may have to prefix the variables item_name,item_quantity,item_price with the self keyword to access the corresponding class variables. Something like self.variable_name.Parthipan Natkunam– Parthipan Natkunam2016年11月30日 04:56:58 +00:00Commented Nov 30, 2016 at 4:56
-
I suppose your issue is resolved, please mark the answer as accepted. @JohnWalkerJarvis– Jarvis2016年11月30日 06:07:13 +00:00Commented Nov 30, 2016 at 6:07
1 Answer 1
You have some issues in your print_item_cost method, it should be like this :
def print_item_cost(self):
total = self.item_quantity * self.item_price
print('%s %d @ $%f = $%f' % (self.item_name, self.item_quantity, self.item_price, total))
You refer to a class attribute like this : self.attr
answered Nov 30, 2016 at 4:50
Jarvis
8,6023 gold badges33 silver badges61 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Sighonide
If the above has solved it, ignore this: Assuming you are running this in a child process (assuming as this is used:
if __name__ == "__main__":), then you might need to flush sys.stdout by calling sys.stdout.flush() in the above code (after you've called all your print functions). This is due to the output of the child process being buffered. Ignore this if you aren't using multiple processesJarvis
There is no mention of multi-programming as far as I know, and as far as I know using
__name__ == "main" is a common thing. There were other issues in the OP's code. You can see my answer and check that for yourself. (and hopefully upvote it too :) ) @SighonideSighonide
I agreed with your answer :) I was only covering all bases as
__name__ == "main" might suggest multiprocessing. But now that I think about it, he probably just wants to be able to import the class and function definition without running main() as well as be able to run it all from scratch : / lollang-py