Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner Getting an error
#1
I'm following this video series but I seem to have messed up at some point. The error is at line 70. Can anyone spot where I have gone wrong?

import os
import datetime
from product import product
from city import City

MENU_DIVIDER = "-----------------------------"
GAME_TITLE = "Python Pirate Trader"

def welcome_message():
    print("Welcome to Pirate Trader")

def get_firm_name():
    #firm_name = input ("Please enter your firm: ")
    return "Testing firm"

def get_starting_options():
    #starting_options = input("How do you wish to start. 1) Cash & Debt 2) Cannons and no Debt.:")
    starting_options = "1"
    if starting_options == "1":
        opts = (250,250,0)
    else:
        opts = (0,0,5)
    return opts

def leave_port(city_list, current_date):
    i = 1
    for city in city_list:
        print("{0}) {1}".format (i,city.name))
        i += 1
    select_city = input ("Which city do you wish to go to?: ")
    current_date += datetime.timedelta(days=1)
    return city_list[int(select_city)-1], current_date
def buy():
    input("What do you want to buy?")

def sell():
    input("What do you want to sell?")
def visit_bank():
    input("How much to transfer to the bank?:")
def display_products():
    for product in Product.products:
            print(product.name + "--" + str(product.price))


class product(object):
    products = []
    def __init__(self, name, minprice, maxprice):
        self.name = name
        self.minprice = minprice
        self.maxprice = maxprice
        self.price = random.randint(self.minprice,self.maxprice)
        @classmethod
        def create_products(cls):
            cls.products.append(Product("General Goods", 3, 20))
            cls.products.append(Product("Arms", 3, 20))

class City(object):
    cities = []
    def __init__(self, name, has_warehouse, has_bank):
        self.name = name
        self.has_warehouse = has_warehouse
        self.has_bank= has_bank
    @classmethod
    def create_cities(cls):
        cls.cities.append(City("Hong Kong",True, True))
        cls.cities.append(City("Shanghai", False, False))
        cls.cities.append(City("London", False, False))

'#' # Create Products
Product.create_products()
City.create_cities()

'#'# Start Game
welcome_message()
firm_name = get_firm_name()
cash, debt, cannons = get_starting_options()

current_city = City.cities[0]
game_running = True

current_date = datetime.datetime(1820,1,1,0,0,0)
while game_running:
    '#'# Display Main Game Interface
    os.system("cls")
    # Stuff to Debug

    # ---------------
    print(MENU_DIVIDER)
    print(GAME_TITLE)
    print(MENU_DIVIDER)

    print("Firm Name: %s" % firm_name)
    print("Cash: {}".format(cash))
    print(f"Debt: {debt}")
    print("Cannons: %d" % cannons)
    print("City: %s" % current_city["City.name"])
    print("Date: {:%B %d, %Y}".format(current_date))
    print(MENU_DIVIDER)
    print("------City Products-----")
    display_products()
    has_bank_string = ""
    if current_city.has_bank == True:
        has_bank_string = "V)isit the Bank,"
    print("Menu: L)eave Port, B)uy, S)ell, T)ransfer Warehouse, %s Q)uit" % has_bank_string)
    menu_option = input("What is your option")
    if menu_option == "L":
        current_city, current_date = leave_port(City.cities, current_date)
    elif menu_option == "B":
        buy()
    elif menu_option == "S":
        sell()
    elif menu_option == "V" and current_city.has_bank == True:
        visit_bank()
    elif menu_option == "Q":
        game_running = False
Reply
#2
What is the full traceback of your error? Without that we are just guessing

this is not a legit comment
Quote:'#' # Create Products

You only need
# Create Products
you import product (lowercase) but use it uppercased
Quote:
from product import product
...
Product.create_products()

You are redefining product on line 45

and your method is misaligned:
class Product(object):
    products = []
    def __init__(self, name, minprice, maxprice):
        self.name = name
        self.minprice = minprice
        self.maxprice = maxprice
        self.price = random.randint(self.minprice,self.maxprice)
        @classmethod
        def create_products(cls):
            cls.products.append(Product("General Goods", 3, 20))
            cls.products.append(Product("Arms", 3, 20))
Recommended Tutorials:
Reply
#3
The error is: NameError: name 'Product' is not defined
Reply
#4
I assume you meant to do this?
from product import Product
Recommended Tutorials:
Reply
#5
I have made that change before and received this afterwards: ModuleNotFoundError: No module named 'product'
Reply
#6
This error:
Error:
Traceback (most recent call last): File "test11.py", line 71, in <module> Product.create_products() NameError: name 'Product' is not defined
is referring to the fact that on this line:
Product.create_products()
Product is undefined.

I dont see anywhere in your code where Product is defined. I only see product. They are two different things.

Either you are not importing it right or your class is lowercase.

In the module product.py change the class to
class Product(object):
instead of
class product(object):
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner Python Error ianmac88 4 1,304 Sep-05-2022, 12:30 PM
Last Post: jefsummers
  Beginner having Syntax Error problem RyanHo 3 2,358 Sep-10-2020, 08:33 AM
Last Post: cnull
  Beginner - simple package installation error mefeng2008 0 1,713 Mar-13-2020, 09:17 AM
Last Post: mefeng2008
  [split] Python beginner: Weird Syntax Error mnsaathvika 1 2,119 Jul-22-2019, 06:14 AM
Last Post: buran
  Beginner Level - Name Error Aniket 11 4,999 Apr-11-2019, 07:53 PM
Last Post: loomski
  Python beginner: Weird Syntax Error mentoly 5 10,297 Oct-13-2017, 08:06 AM
Last Post: gruntfutuk
  Display error, beginner help :( Armandas 1 3,181 May-02-2017, 08:39 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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