[Tutor] I am having trouble getting some associations to work

Dennis Lee Bieber wlfraed at ix.netcom.com
Mon Feb 28 12:35:28 EST 2022


On Sun, 27 Feb 2022 14:09:28 -0600, Tom Linsenmeyer
<tlinsenmeyer at gmail.com> declaimed the following:

>I cannot get either self or getmakecar and getmaketruck to associate with
>self or def getmakecar() and getmaketruck(). I can get one or the other not
>both
>
>Can you please help me
>Thank you

	Warning: I'm likely to get a bit advanced and picky over some this...

>
>
>import sys
>import time
>
>
>#A program to make a virtual garage
>

	So where is a Garage class? A garage /holds/ vehicles, it is not a
vehicle itself. Though you could get by with a list or dictionary as a
container of vehicles -- it depends upon what operations you need to
implement.

	What are the operations one performs with a garage instance?

>class Vehicles:

	Classes are commonly singular -- each instance of a class represents,
well ONE UNIQUE instance of the class...
>
>
>  def __init__(self, fueltype, make, carmodel, truckmodel, color):
>        """ initialize attributes."""
>        self.carmodel = carmodel
>        self.truckmodel= truckmodel
>        self.Make_ = make
>        self.Color_ = color
>        self.fueltype_ = fueltype

... and this part appears to represent ONE UNIQUE Vehicle. But what is with
both carmodel and truckmodel? A vehicle is one or the other, it does not
represent both at the same instant of time.

>        make = {'Make1' : 'Chevrolet', 'Make2' : 'Dodge', 'Make3' : 'Ford'}
>        carmodel = {'Cmodel' : 'Camaro', "Cmodel2" : 'Corvette',
>'Cmodel3' : 'Charger', 'Cmodel4' : 'Challenger'}
>        truckmodel = {'Tmodel1' : 'Silverado', 'Tmodel2' :
>'Tahoe','Tmodel3' : 'Ram-1500', 'Tmodel4' :'Ram-2500',' Tmodel5' :'F-150',
>'Model_' : 'F-250'}
>        fueltype = {'fuelType1' : 'Gasoline', 'fuelType2' : 'Diesel',
>'fuelType3' : 'FlexFuel'}
>        color = {'Color1' : 'White', 'Color2' : 'Black', 'Color3' : 'Blue',
>'Color4' : 'Red', 'Color5' : 'Yellow'}
>

	What are all these? Besides being local to __init__ (meaning they are
deleted when __init__ exits), they merely replace the input parameters
required when creating an instance (and which you've already saved /in/ the
instance). The use of a dictionary in which the keys are just "info#" is
also rather perplexing. Is someone actually going to input "Make1" on some
screen to select "Chevrolet"? A simple list ["Chevrolet", "Dodge", "Ford"]
can be indexed much faster than having to parse/validate string input (note
that your "Camaro" doesn't have a # on the key).

	Also note that there is nothing implicit in those that would prevent
one picking, say, "Dodge"/"Camaro"/"Diesel".


	These items look like they should be part of the user
interface/database of features, but are not, themselves, part of any
particular vehicle. Furthermore, they are not what I'd consider "vehicle"
nor "garage" -- they look more like some ordering system to specify a
vehicle, which may or may not be available in the "garage".

>  def mainmenu(self):
>
>          print("Welcome to your garage! Please make a selection")
>          print("1: Car")
>          print("2: Truck")
>          print("3: Quit")
>          choice = int(input())
>          if choice == 1:

	Note that, should you attempt to expand this later (say you add
motorcycles) you have to redo ALL the logic for your menu scheme. You might
want to consider designing a generic menu handler that works for all menus.
*** see bottom


>            print("Car")
>            getmaker()

	There is no getmaker() defined, but if it is supposed to be a method of
the Vehicles class, it needs to be called as

		self.getmaker()

	Same for anything else that is a method defined within the class.

>          #elif choice == 2:
>           # getmakeTruck()
>          elif choice == 3:
>              quit()
>          else:
>              print ("Select either Car, Truck or Quit")
>
>
>  def getmakecar(self):

	By the time you could use this method, you have already specified all
the information -- since the call to create an instance of Vehicles looks
like:

aVehicle = Vehicles(fueltype, make, carmodel, truckmodel, color)

{see my above comment about having both car and truck model AT THE SAME
TIME|


***
	Consider (not completely worked out or tested):


def doMenu(items):
	while True:
		for i, itm in enumerate(items):
			print("%5s: %s" % (i, itm[0]))
		inp = input("Enter selection number (blank line to exit)=>
").strip()
		if inp:
			#not a blank line
			chc = int(inp)
			if chc >= 0 and chc < len(items):
				#valid range for selection
				#call the handler function for the selected item
				thing = items[chc][1]()
				return (items[chc][0], thing)
			else:
				print("Selection is not valid -- try again\n\n")
		else:
			return None


def makeCar():
	makes = [ ("Chevrolet", makeChevy), ("Ford", makeFord), ("Dodge",
makeDodge) ]
	thing = doMenu(makes)
	if thing is None:
		print("User canceled procedure")
	else:
		print("Selected is %s" % thing[0])
		return thing[1]

def makeTruck():
	pass

categories = [ ("Car", makeCar), ("Truck", makeTruck) ]

selection = doMenu(categories)
if selection is None:
	print("User aborted operation")
else:
	print ("Selection is %s" % selection[0])
	vehicle = selection[1]
	


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/



More information about the Tutor mailing list