Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
calling a variable by name
#1
my code is: 

class char(object):
    'chactors'
 
    def __init__(self,name,d,i):
        self.name = name
        self.d = int(d)
    
    def namechar(self):
        print self.name
 
    def action(self,skill):
        if in listd:

            stat = d
        elsif in listi:
            stat =i
        else:
            pass
        t = skill + stat
        print t

    listd =['mov']
    listi =['act']

char1 = char('q','1','2')
 
setattr(char1, "mov", 10)
setattr(char1,'act', 100)
 
print char1.mov
 
char1.action('mov')
char1.action('act')
What I am trying to do:

to have a method/function that will add 2 valuables together and print the result.how ever each time i call one variable i want to add it add it to an other variable no mater which object it call on. for example i want to all add the variable 'mov' to be added to d and the variable 'act' to i. so it seams unnecessary to enter both.  instead  have the program figure out the other. for example if i called i call it on 'mov' for to figure out i needs to add it to 'd'.I am trying to do to it with a if then statement and  looking up a variable in a list. how ever i need to to know it looking for variable name and not the value. from what i here it hard to do how do i do or achieve the same things with a different means.
Reply
#2
I have read through your question a few times.
Then looked at the code and see loads of things wrong with it.
Then I decided I don't know how to offer help as I don't understand what you are trying to do.
Please read How to ask Smart Questions.
Reply
#3
I think how to ask smart questions is valuable feedback, but I'm concerned that there will be a language barrier here anyway.

sunhear: I suggest you try to show us what you want with very simple code. Don't show any implementation, just show what you want the function call and result to look like. If we can make sense of that, we might be able to provide an answer for you without having to worry about too much cruft in the question.
Reply
#4
Well, since no one can figure out the question, let's look at some of the obvious problems in your code.

You don't save the i parameter you pass to __init__. Since your example passes '2' to i, you probably want to add this to __init__:

self.i = int(i)
In the action method, you have this line:

if in listd:
The 'in' operator is a binary operator. You need a value on each side of it. I'm guessing you are trying to check if the skill parameter is in listd, which would be:

if skill in self.listd:
Note the self I added. You need that to access the attributes of the class instance that you set up in __init__. The listd variable is actually an attribute of the class, not the instance, but we can access it using self as well. Likewise you will want the next line to be:

stat = self.d
Note that you want the same changes for the elsif line and the line after it. And note that elsif should be elif (no 's').

t = skill + stat
The above line is a problem, since the skill parameter is a string and the stat local variable is an integer. Those two things don't add. Given what you are passing to skill, and your two setattr lines (which are just a really bad idea), I'm guessing you want:

t = self.__getattr__(skill) + stat
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
i am trying to implement the changes in the code. I am new at programming objects and classes.

The original question was how to right a function/method to add 2 variables together when only 1 is passed as an argument.
Reply
#6
No offense but your code is so bad and filled with unrelated content regarding the question, that it is more confusing than helpful. To give a proper answer would require a complete new code snippet from us. Thus we would need
Quote:just show what you want the function call and result to look like
Recommended Tutorials:
Reply
#7
(Oct-16-2016, 07:46 PM)sunhear Wrote: i am trying to implement the changes in the code. I am new at programming objects and classes.

The original question was how to right a function/method to add 2 variables together when only 1 is passed as an argument.

By using "self" to reference the other instance variables in your object.  Here, we create an object, then instantiate it, and call a method of that instance, passing only one argument.  So we do math with two variables, even though we only passed one.
>>> class Spam(object):
...   def __init__(self):
...     self.eggs = 42
...   def add(self, other):
...     return self.eggs + other
...
>>> fork = Spam()
>>> fork.add(9)
51
Reply
#8
Hmm. So you take the answer to life, the universe, and everything; add the number of Supreme Court justices; and the result is the location where the Army stores the alien technology. Coincidence? I think not.
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
  Calling functions by making part of their name with variable crouzilles 4 853 Nov-02-2023, 12:25 PM
Last Post: noisefloor
  Calling a base class variable from an inherited class CompleteNewb 3 1,700 Jan-20-2022, 04:50 AM
Last Post: CompleteNewb
Question Calling on a Variable using other Variables jacknewport 4 2,010 Jul-23-2021, 04:18 PM
Last Post: jacknewport
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 2,257 Apr-06-2020, 09:14 AM
Last Post: bowlofred
  calling a variable in a for loop in windows dwaynes 1 1,816 Apr-02-2020, 05:21 PM
Last Post: mcmxl22
  calling an object variable in a dictionary sunhear 3 4,310 Dec-30-2016, 05:28 PM
Last Post: sunhear

Forum Jump:

User Panel Messages

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