Input to call class instance?

I was hoping to get help with the challenge for challenge 1 in the Python 3 Course. This is my second time going through this section and I wanted to challenge myself to use everything I had learned so far.

This is the code:

sales_tax = 0.88
input_track = []

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

def Input_Chain():
  customer_input = input("Select Item")
  if customer_input == 0:
    Checkout()
  else:
    input_track.append(customer_input)
    Input_Chain()

def Checkout():
  total = 0
  itemization = ""
  try:
    for i in input_track:
      total += i.price
      itemization += i.name + ": "
      itemization += i.description + "\n"
  except:
    print("An item is not on our list")
  print("Total Before Tax:" + str(total))
  tax = total * sales_tax
  total += tax
  print(itemization)
  print("Total After Tax:" + str(total))

Lovely_Loveseat = Item("Lovely Loveseat", "Lovely Loveseat. Tufted polyester blend on wood. 32 inches high x 40 inches deep. Red or white.",254.00)
Stylish_Settee = Item("Stylish Settee", "Stylish Settee. Faux leather on birch. 29.50 inches high x 54.75 inches wide x 28 inches deep. Black.",180.50)
Luxurious_Lamp = Item("Luxurious Lamp", "Luxurious Lamp. Glass and iron. 36 inches tall. Brown with cream shade.", 52.15)

print(type(Lovely_Loveseat))

Input_Chain()

I tested this code, and everything works up to the part above where it says customer_input = input(“Select Item”). This always reads as a string and I have been unable to find a way to convert it to a readable format to call the class instances.

I was hoping someone could offer a solution to the problem, or perhaps introduce a different way to tackle the problem.

Thank you so much!

Note: trying the code in codecademy will raise an EOD? warning which I found was associated to not having a terminal for input. I’ve been using the code in an IDE as a workaround.

If the input is a string, you can convert it to an int if that’s what you need (or check against a string).

1 Like

Hi! I appreciate the quick response!

Unfortunately integer will not work for what is needed!

The problem is that I want to create a code that takes user input and can call a class instance to get its price, name, and description.

Right now when the code passes it will correctly call for input. So if I were to type the class instance: Lovely_Loveseat, and then the exit input 0. the function Checkout() should run (It doesn’t for a reason I am about to explain) and get the instance variables needed to get a total and the itemization.

The problem is that instead of the user input being Lovely_Loveseat. It reads as “Lovely_Loveseat” and instead of 0 it would read as “0”.

The int() would fix the 0 issue but raise a literal error when applied to the “Lovely_Loveseat”

Hi again,

I think this could be simplified taking a step back. The immediate hack is to write a helper function that says try to take a num, if it’s not a num process it as a string.

A (possibly) simpler thing to do is to unify your expected input in a way that’s reasonable to deal with. You could present the user with a multiple number of options and then go from there (that’s just one idea).

  1. couch
  2. lamp
  3. umm shades?
  4. (next page)
  5. checkout
  6. cancel interaction, quit

So I spent the past few days looking at things that I normally work with and try to understand the how behind them. Your solution to present the user with multiple options was a recurring theme throughout.

Thank you so much for your help!