[Tutor] Using a string to get at a Variable
isrgish at fusemail.com
Mon Jan 19 00:46:58 EST 2004
>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.
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
More information about the Tutor