``````class calculator:
def __init__(self, num, operator):
self.__num = num
self.__operator = operator

def changeNum(self, newNum):
a = newNum
return a

def getNum(self):
return self.__num``````
``````def click(num):
if num == "one":
v = "1"
elif num == "two":
v = "2"
elif num == "three":
v = "3"
elif num == "four":
v = "4"
elif num == "five":
v = "5"
elif num == "six":
v = "6"
elif num == "seven":
v = "7"
elif num == "eight":
v =  "8"
elif num == "nine":
v = "9"
self.__num1 = self.__num1+v

calculator = calculatorClass.calculator(self.__num1, self.__sign)

num1 = calculator.getNum()
num2 = calculator.changeNum(self.__num1)
print(num1, num2)

self.display.config(text = "a")``````

i trying to define two number
but when num1 = 3 ...num2 is = 3...how can i solve this problem
how can i make them into two separate number;

also i require to define a class for this calculator..can sum1 help me or give me sum hints how i going to define the class?

Wildplace, your code is rather strange. I think that you are trying to do something well above your level of knowledge at the moment. Perhaps the best thing you can do is get a good Python tutorial and work through the chapters on functions and object orientation.

Anyway, lets look at your code a little.

First off, how do you call the function. If it is a calculator, I am not clear on where you think the two numbers or the operator comes from?

Basically I guess that you want to call the function with one value, and it should be a text value for a number between one and ten? So what do you do with values higher than 9. Why no zeros?

Okay, then you want to instantiate the class.

``````calculator = calculatorClass.calculator(self.__num1, self.__sign)
``````

Okay, I am going to put the class and the function in the same file, so loose the calculatorClass (I assume that that was an import?)

You don't instantiate a class with the 'self' reverence...Python puts the self in for you. Self, itself, is not a reserved word in Python, it is just a convention. So if you use 'self' outside of your class, it doesn't mean anything. (note: if you meant click to be a method of calculator then I am wrong, but since you gave the two blocks of code apart, I assume not.)

``````calculator = calculator(__num1, __sign)
``````

From that, it should also be clear that this line `self.__num1 = self.__num1+v` should be `__num1 = __num1+v`

But that `__num1` doesn't mean anything. You need to create it in order to add it to itself. Lets put it at the beginning of the function.

``````def click(num):
__num1 = ""
if num == "one":
``````

Now if you run the code, if your value doesn't equal any one of the if/elifs then you end up with an error - because v isn't defined when you try to add it to `__num1`. You can either assign it as the final else or before you start that whole block.

``````def click(num):
__num1 = ""
v = ""
if num == "one":
``````

Okay, next problem, when you instantiate calculator, don't call the object calculator, otherwise it takes it as a reference to the class and gives you an error. lets call it calc:

Now, when you create the object calc you want to pass a `__sign` to the class (you want to, that is, if you don't want an error to crash your script.)

Why not add this sign to the function, so that when you call the function you can pass a sign to it.

``````calculator = calculatorClass.calculator(self.__num1, self.__sign)
``````

Okay, if you read all the above carefully, then you will understand why your method calls get changed like this.

``````        num1 = calc.getNum()
num2 = calc.changeNum(__num1)
``````

I have no clue what this line does or is meant to do:

``````#
self.display.config(text = "a")
``````

So I will just delete it.

Now, how about a way to call your function, `click('two', '+')`. Just to make it a little more fun, import random and do this:

``````if __name__ == "__main__":

s = ["+", "-", "", "*"]
vList = ['one', 'three', 'eight', 'two']
for v in vList:
Choose_sign = random.randint(0,3)
click(v, s[Choose_sign])
``````

Now you can run it, and it will not bring up an error. But it still does nothing. Lets brake down the class:

``````class calculator:
def __init__(self, num, operator):
self.__num = num
self.__operator = operator
``````

Okay, when you instantiate the class, you give it one number as a string and one operator, also as a string. After that, your class does nothing with the operator, so I am not sure why it was passed in the first place. Lets make it do something.

Then we have the methods. The first one:

``````    def changeNum(self, newNum):
a = newNum
return a
``````

Gets passed a value, assigns it to a and returns it. It does nothing.

The second:

``````    def getNum(self):
return self.__num
``````

Does nothing, it only returns a value that was already assigned in the `__init__` method. So basically your class does nothing but pass on the values it was given. Lets re-write it to do something like a calculator.

``````#! /usr/bin/python

# wildplace.py

import random
import math

class calculator:
def __init__(self, num, operator):
self.__num = num
self.__operator = operator
self.nums = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']

def generateNum2(self):
a = random.randint(0, 8)
num2 = self.nums[a]

return num2

def calculateIt(self):
val1 = (self.nums.index(self.__num)) + 1
__num2 = self.generateNum2()
val2 = (self.nums.index(__num2)) + 1

if self.__operator == "plus":
r = val1 + val2
elif self.__operator == "minus":
r = val1 - val2
elif self.__operator == "divided":
r = val1 / val2
elif self.__operator == "times":
r = val1 * val2
try: x = self.nums[r-1]
except: x = r
ReturnString = "%s %s %s equals %s" % (self.__num, self.__operator, __num2, str(x))
return ReturnString
def click(num, sign):

calc = calculator(num, sign)
print calc.calculateIt()

if __name__ == "__main__":

sgn = ["plus", "minus", "divided", "times"]
vList = ['one', 'four', 'six', 'nine']
for v in vList:
Choose_sign = random.randint(0,3)
s = sgn[Choose_sign]
click(v, s)
``````

Note the try/except is there because our list of numbers to names runs out after 9. Okay, if you get negative numbers you are going to get funny things (-1 will give you nine...because the list starts to count backwards when you give it a negative index value.) If this is the kind of thing you were trying to work out, you should be able to work on it from there. Otherwise you need to perhaps rephrase your question, and clean up your code a little.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.