[Tutor] Using a string to get at a Variable

Isr Gish isrgish at fusemail.com
Mon Jan 19 00:46:58 EST 2004



-----Original Message-----
   >But this is what I'm looking for.
   >
   >apples = 10
   >grapes = 5
   >pears = 8
   >fruits = ['apples', 'grapes', 'pears']
   >Now Iswant to be able to do something like this.
   >for fruit in fruits:
   >	print 'You heve %d' % (fruit), fruit
   >
   >And the output should look like this:
   >You have 10 apples
   >You have 5 grapes
   >You have 8 pears
   >
   >Thanks for the reply and if anyone can help it would be appreciated.
   >
   >Isr Gish

I think I found the answer to use eval(...)
apples = 10
grapes = 5
pears = 8
fruits = ['apples', 'grapes', 'pears']
Now Iswant to be able to do something like this.
for fruit in fruits:
	print 'You heve %d' % eval(fruit), fruit

Isr




More information about the Tutor mailing list