Python Forum
Calling an class attribute via a separate attribute in input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling an class attribute via a separate attribute in input
#1
Hi, am trying to call a class attribute but am unable to get it to work. When the user inputs the Item.code into a float input, I want to display

"You have selected (Item.name) which is (Item.price). Currently (Item.amount) in stock."

I have read through the basics of Classes on the website, and Inheritance and Overloading. But I can't work out how to refer to the other Item attributes. I can always revert back to just lists, but I know that isn't good code.

Any help would be appreciated.

Code below

class Item:
    def __init__(self, code, name, price, amount):
        self.code = code
        self.name = name
        self.price = price
        self.amount = amount


class VendingMachine:

    def __init__(self):

        self.items = [
            Item(1, "Tea", 0.50, 10),
            Item(2, "Coffee", 1.00, 10),
            Item(3, "Coke", 1.50, 6),
            Item(4, "Orange Juice", 1.00, 5)
        ]
Reply


Messages In This Thread
Calling an class attribute via a separate attribute in input - by wiggles - Apr-04-2020, 07:50 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  AttributeError: 'str' object has no attribute 'size' russoj5 4 7,592 Nov-15-2020, 11:43 PM
Last Post: deanhystad
  Object has no attribute 'replaceall' ? peterp 2 7,312 Nov-10-2020, 09:23 PM
Last Post: buran
  AttributeError: 'list' object has no attribute 'g_s' NitinL 6 3,515 Mar-31-2020, 10:24 AM
Last Post: pyzyx3qwerty
  How can you add an attribute to a function within a class? sjaakthomassen 2 1,966 Mar-18-2020, 12:27 AM
Last Post: scidam
  Attribute error in code 3DEN 1 2,506 Dec-09-2019, 10:43 AM
Last Post: buran
  ERROR NoneType object has no attribute content denizkb 1 2,680 Nov-21-2019, 01:18 PM
Last Post: denizkb
  Attribute Error DiceMann 1 1,987 Nov-01-2019, 06:54 AM
Last Post: buran
  AttributeError: 'tuple' object has no attribute 'move' senfik99 2 4,103 Feb-26-2019, 12:42 PM
Last Post: stullis
  Class and calling a method Zatoichi 3 3,167 Mar-13-2018, 08:44 PM
Last Post: Zatoichi
  text = str(text.encode('utf-8')) AttributeError: 'float' object has no attribute 'enc ulrich48155 2 8,855 Jul-31-2017, 05:21 PM
Last Post: ulrich48155

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020