Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to print variable name?
#11
(Oct-20-2019, 04:07 PM)mayadob Wrote: What if I call it with:

length(c, d)
length(e,f)

As Ichabod said:

(Oct-20-2019, 05:45 PM)ichabod801 Wrote: [ ... ] somehow you need to store the name.

so maybe you could store the names of the variables and the values of those variables in a list, named, let's say data1, where after the name of the variable between quotes, we put the value of that variable, like the following:

data1 = ['a', 5, 'b', 3, 'c', 7, 'd', 2, 'e', 1, 'f', 9]
As you want to manage the names of those variables and their values within a function, I created a generic function which I named length(x, y) for a generic couple of variables named x, y to add the values of those variables, while at the same time, using also the names of those variables.

If we have a certain character x in a list named data1, we can get its position (index) in the list, using the command data1.index(x). If we called this value, xVariableValue, we can use the following expression:
xVariableValue = data1.index(x)
This expression is quite useful for our purpose, as it gives us the index(position) of the character x (meaning a generic variable) in the list named data1 and we store it in what we called xVariableValue. Therefore, if we asked for the item in that position in that list, we have the name of the variable. According to our convention when we created the list, if the name of a variable is at an index of xVariableValue, its value is the next item, that is to say, its value is at an index of xVariableValue+1. So:

data1[xVariableValue] gives us the name of the generic variable x,
data1[xVariableValue+1] gives us the value of the generic variable x,
data1[yVariableValue] gives us the name of the generic variable y,
data1[yVariableValue+1] gives us the value of the generic variable y.

Applying the same logic, we can then fill the missing coding bits of our function and program, applying the generic length(x, y) function, for example, to 4 pairs of variables from our list:

data1 = ['a', 5, 'b', 3, 'c', 7, 'd', 2, 'e', 1, 'f', 9]

def length(x, y):
    xVariableValue = data1.index(x)
    yVariableValue = data1.index(y)
    print(f'''\n{data1[xVariableValue]} = {data1[xVariableValue+1]}\n\
{data1[yVariableValue]} = {data1[yVariableValue+1]}\n\
The length of '{data1[xVariableValue]}{data1[yVariableValue]}' is {data1[xVariableValue+1] + data1[yVariableValue+1]}.\n''')
             
length('a', 'b')
length('c', 'd')
length('e', 'f')
length('c', 'f')
and that little program produces the following output:
Output:
a = 5 b = 3 The length of 'ab' is 8. c = 7 d = 2 The length of 'cd' is 9. e = 1 f = 9 The length of 'ef' is 10. c = 7 f = 9 The length of 'cf' is 16.
I hope that this is what you wanted and that I have explained it more clearly now.

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#12
It would be better to do it in a dictionary rather than a list:

POINTS = {'a': 5: 'b': 3, 'c': 7, 'd': 2, 'e': 1, 'f': 9}
Then the function is simpler:

def length(x, y):
    print(f'{x} = {POINTS[x]}')
    print(f'{y} = {POINTS[y]}')
    print(f'The length of {x}{y} is {POINTS[x] + POINTS[y]}')
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Print variable without '' and spaces arnonim 1 721 Jan-30-2023, 05:23 PM
Last Post: deanhystad
  Problem with print variable in print.cell (fpdf) muconi 0 653 Dec-25-2022, 02:24 PM
Last Post: muconi
  Remove a space between a string and variable in print sie 5 1,758 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  Can you print a string variable to printer hammer 2 1,928 Apr-30-2022, 11:48 PM
Last Post: hammer
  Print variable between " paulo79 5 1,688 Mar-11-2022, 05:16 PM
Last Post: deanhystad
  When I print a key from dict it prints it but when I try to assign it to a variable I stefanvelikov 3 2,347 Nov-27-2020, 01:29 PM
Last Post: stefanvelikov
  Print variable values from a list of variables xnightwingx 3 2,614 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Print part of a variable rs74 4 2,567 Jul-27-2020, 04:39 PM
Last Post: rs74
  How do I print a returned variable calculated in another function? RedSkeleton007 3 3,525 Jul-10-2018, 12:10 PM
Last Post: buran
  Empty variable when using print before readline fstefanov 3 3,648 Oct-23-2017, 02:22 AM
Last Post: fstefanov

Forum Jump:

User Panel Messages

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