-1

I am currently teaching myself Python and I am currently getting indentation errors in my program. When I fix the error on line 7 and indent the line my input number_input gets grayed out by my IDE (Pycharm community if that matters) so it is null and other errors appear after fixing the error. My program contains a class called RetailItem in the file q05 and a main called q05_1test. The goal of my program is that the user enters either 1, 2, or 3 and displays the corresponding output. For example when the user enters 2 the program should result in Designer Jeans (description), 40 (units), and 34.95 (price) being displayed.

File q05.py (Class):

class RetailItem:
    def __init__(self, descr, units, price):
        self.__descr = descr
        self.__units = units
        self.__price = price

    # mutator methods
    def set_descr(self, descr):
        self.__descr = descr

    def set_units(self, units):
        self.__units

    def set_price(self, price):
        self.__price

        # accessor methods

    def show_descr(self):
        return self.__descr

    def show_units(self):
        return self.__units

    def show_price(self):
        return self.__price

        # set string method

    def __str__(self):
        return "Description: " + self.__descr + \
               "\nUnits in Inventory: " + str(self.__units) + \
               "\nPrice: $" + str(self.__price)

Main q05_1test


import q05.py

def main():
#user enters number that determienes what results will be displayed
number_input = input("Enter 1 for item 1 and vise versa for items 2 and 3")
    items_list = []

#list
descr_list = ["Jacket", "Designer Jeans", "Shirt"]
units_list = [12, 40, 20]
price_list = [59.95, 34.95, 24.95]


for number in range(0, 4):
            if number_input == 1:
                for i in range(0, 3, 1):
                    descr = descr_list[i]
                    units = units_list[i]
                    price = price_list[i]
            if number_input == 2:
                for i in range(1, 3, 1):
                    descr = descr_list[i]
                    units = units_list[i]
                    price = price_list[i]
            if number_input == 3:
                for i in range(2, 3, 1):
                    descr = descr_list[i]
                    units = units_list[i]
                    price = price_list[i]

                    item = q05.RetailItem(descr, units, price)

                    items_list = []
                    items_list.append(item)

                for item in items_list:
                    print(item)
                    print()

if __name__ == "__main__":
    main()

vvvvv
  • 25,404
  • 19
  • 49
  • 81
fred
  • 9
  • 2
  • Everything that is part of the `main()` function needs to be indented at minimum one tab. Python's scoping is entirely dictated by indentation. Also, `input()` returns a string and your `if` statements will not work properly because you'll be doing e.g., `"3" == 3`. – ddejohn Nov 11 '21 at 05:19

2 Answers2

0

You need to indent after starting a function.

Changes in q05_1test.py:

import q05

def main():
    #user enters number that determienes what results will be displayed
    number_input = input("Enter 1 for item 1 and vise versa for items 2 and 3")
    items_list = []

    #list
    descr_list = ["Jacket", "Designer Jeans", "Shirt"]
    units_list = [12, 40, 20]
    price_list = [59.95, 34.95, 24.95]


    for number in range(0, 4):
        if number_input == 1:
            for i in range(0, 3, 1):
                descr = descr_list[i]
                units = units_list[i]
                price = price_list[i]
        if number_input == 2:
            for i in range(1, 3, 1):
                descr = descr_list[i]
                units = units_list[i]
                price = price_list[i]
        if number_input == 3:
            for i in range(2, 3, 1):
                descr = descr_list[i]
                units = units_list[i]
                price = price_list[i]

                item = q05.RetailItem(descr, units, price)

                items_list = []
                items_list.append(item)

            for item in items_list:
                print(item)
                print()

if __name__ == "__main__":
    main()
Anurag
  • 308
  • 2
  • 13
0

There are 2 problems here.

Order: You are running the for loop before the main function is called, so number_input isn't set when it's trying to be accessed. To fix this, you must move the bottom two lines above the for loop.

However, that won't completely solve the problem. This is because of scope. When you set a variable while inside a function, that change is only visible in the function itself. To fix this, use the global keyword:

(...)
def main():
    global number_input, items_list
(...)

See here to understand how scope works in Python.

qucchia
  • 95
  • 7