P: 1
|
Hi,
I have in my program a list of lists of variables. These variables change as the program runs. I would like to be able to link meaningful names to the variables. So, say I have a list of quantities of different coins in my possession
where the entries are the quantities of pennies, nickels, dimes, and quarters I have, respectively. Can I assign variable names to the entries so that if, in the course of my program, it simulates a purchase for 5 cents- -
myChange [3] = myChange [3] - 1
-
myChange [2] = myChange [2] + 2
-
I can say -
print quarters
-
print dimes
-
And get the following output?
0
2
Thanks for the help; sorry if my terminology is bad as I am new to programming!
| |
Share this Question
Expert 5K+
P: 6,596
|
Hi,
I have in my program a list of lists of variables. These variables change as the program runs. I would like to be able to link meaningful names to the variables. So, say I have a list of quantities of different coins in my possession
where the entries are the quantities of pennies, nickels, dimes, and quarters I have, respectively. Can I assign variable names to the entries so that if, in the course of my program, it simulates a purchase for 5 cents- -
myChange [3] = myChange [3] - 1
-
myChange [2] = myChange [2] + 2
-
I can say -
print quarters
-
print dimes
-
And get the following output?
0
2
Thanks for the help; sorry if my terminology is bad as I am new to programming!
Your terminology (and use of this site) is excellent for a beginner!
You could achive 100% of your goal (getting a reference to an array item) using an extension called scipy. A 99% solution with native lists requires that you unpack the list each time you want to use it. It's a technique that I use often: -
>>> aList = [1, 2, 3, 4]
-
>>> pennies, nickles, dimes, quarters = aList
-
>>> quarters
-
4
-
>>>
| | Expert Mod 2.5K+
P: 2,851
|
You may consider using a class for your program. This is a start: - class myChange(object):
-
-
def __init__(self, penny=0, nickel=0, dime=0, quarter=0):
-
self.penny = penny
-
self.nickel = nickel
-
self.dime = dime
-
self.quarter = quarter
-
self.data = [penny,nickel,dime,quarter]
-
self.__init = 1
-
-
def __getitem__(self, i):
-
return self.data[i]
-
-
def __setitem__(self, i, value):
-
try:
-
object.__setattr__(self, {0:'penny',1:'nickel',2:'dime',3:'quarter'}[i], int(value))
-
object.__setattr__(self, 'data', [int(self.penny), int(self.nickel), int(self.dime), int(self.quarter)])
-
except KeyError:
-
raise IndexError, "Index argument out of range in '%s' __setitem__" % (type(self).__name__)
-
except ValueError:
-
raise ValueError, "Invalid literal in '%s' __setitem__" % (type(self).__name__)
-
-
def __setattr__(self, name, value):
-
if not self.__dict__.has_key('_myChange__init'):
-
return object.__setattr__(self, name, value)
-
elif self.__dict__.has_key(name):
-
object.__setattr__(self, name, value)
-
object.__setattr__(self, 'data', [int(self.penny), int(self.nickel), int(self.dime), int(self.quarter)])
-
else:
-
raise AttributeError, "'%s' object has no attribute '%s'" % (type(self).__name__, name)
Here is some interaction: - >>> myChange(6,4,7,2)
-
<__main__.myChange object at 0x00F972D0>
-
>>> chg = myChange(6,4,7,2)
-
>>> chg.penny
-
6
-
>>> chg.penny = 12
-
>>> chg.data
-
[12, 4, 7, 2]
-
>>> chg.quarter += 7
-
>>> chg.quarter
-
9
-
>>> chg.data
-
[12, 4, 7, 9]
-
>>> chg[3]=3
-
>>> chg.quarter
-
3
-
>>> chg[3]
-
3
-
>>> chg[4]
-
Traceback (most recent call last):
-
File "<interactive input>", line 1, in ?
-
File "C:\SDS2_7.0\macro\Work In Progress\myChange.py", line 13, in __getitem__
-
return self.data[i]
-
IndexError: list index out of range
-
>>>
| | | | Question stats - viewed: 1500
- replies: 2
- date asked: Jul 30 '07
|