Posts: 12
Threads: 5
Joined: Jun 2020
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 22 23:46:47 2020
@author: Lenovo
"""
import pdb
import unittest
import math
class Calculator:
"""
A class is defined for calculator, to preform addition,subtration,multiplication,divison,power and exponent.
"""
def __init__(self,num1,num2):
try:
assert type(num1) == int
assert type(num2) == int
except AssertionError:
print('Invalid parameter type')
raise Exception
self.num1 = num1
self.num2 = num2
def addition(self):
#pdb.set_trace()# we have added a breakpoint here. The code pause execution here.
#print(' Addition')
return (self.num1 + self.num2)
def subtraction(self):
return(self.num1 - self.num2)
def division(self):
return(self.num1 / self.num2)
def moduler(self):
return(self.num1 // self.num2)
def multiplication(self):
return(self.num1 * self.num2)
"""def power(self):
return(self.num1 ** self.num2)
"""
class ScientificCalculator(Calculator): #parent class refrence Calculator
def __init__(self,num1,num2,number,exponent): #should be initialize this function __init__()
super().__init__(num1,num2) # super() will refer paranent class variables
self.number = number
self.exponent = exponent
def logg(self):
#pdb.set_trace() #we have added a breakpoint here. The code pause execution here.
return math.log(self.number,self.exponent)
def power(self):
a = self.number
b = self.exponent
return lambda a,b:a**b
cal= Calculator(num1 = 2,num2 = 2)
print('addition',cal.addition())
sci_cal = ScientificCalculator(num1=1,num2=2,number = 2,exponent = 3)
print('log:',sci_cal.logg())
print('power:',sci_cal.power()) Output: addition 4
log: 0.6309297535714574
power: <function ScientificCalculator.power.<locals>.<lambda> at 0x0000011660DFE798>
Posts: 151
Threads: 3
Joined: Apr 2020
The code lambda parameters: expression creates a lambda function object, so that is what you are returning in your power() function. You need to actually call the lambda function by passing values to it within your return statement.
x = lambda a: a * 2 # defining x as a lambda function
print(x) # printing the function object
print(x(5)) # calling the function with argument '5' and printing the return value Output: <function <lambda> at 0x0363A3D0>
10
Posts: 6,347
Threads: 17
Joined: Feb 2020
Using lambda functions for a calculator is common, but you are using them in the wrong place. A common way to use lambda functions for this task is to make a dictionary that associates a function to execute with an operator. Compare the two calculators below that are the same except one uses lambda to define the functions.
def subtract(a, b):
"""return a-b"""
return a-b
def addition(a, b):
"""return a+b"""
return a+b
def multiplication(a, b):
"""return a*b"""
return a*b
def division(a, b):
"""return a/b. Gotta make lint happy"""
return a/b
operators = {
'+': addition
'-': subtraction
'*': multiplication
'/': division
a, op, b = input('Enter equation: ').split()
print(operators[op](float(a), float(b))) operators = {
'+': lambda a,b: a+b,
'-': lambda a,b: a-b,
'*': lambda a,b: a*b,
'/': lambda a,b: a/b}
a, op, b = input('Enter equation: ').split()
print(operators[op](float(a), float(b))) lambda is used when you don't want a named function. I do not want my calculator program cluttered with named functions that all consist of one line. To use lamda as you do, to create a function when you don't need a function, is nonsensical. You could modify your code to make the lambda work, but what does this accomplish?
def power(number, exponent):
return (lambda a,b:a**b)(number, exponent)
def power(number, exponent):
return number**exponent
Posts: 12
Threads: 5
Joined: Jun 2020
Jul-03-2020, 07:55 PM
(This post was last modified: Jul-03-2020, 07:55 PM by mbilalshafiq.)
Thanks dear , your guidance.
Regards
MB
(Jul-03-2020, 02:39 PM)deanhystad Wrote: Using lambda functions for a calculator is common, but you are using them in the wrong place. A common way to use lambda functions for this task is to make a dictionary that associates a function to execute with an operator. Compare the two calculators below that are the same except one uses lambda to define the functions.
def subtract(a, b):
"""return a-b"""
return a-b
def addition(a, b):
"""return a+b"""
return a+b
def multiplication(a, b):
"""return a*b"""
return a*b
def division(a, b):
"""return a/b. Gotta make lint happy"""
return a/b
operators = {
'+': addition
'-': subtraction
'*': multiplication
'/': division
a, op, b = input('Enter equation: ').split()
print(operators[op](float(a), float(b))) operators = {
'+': lambda a,b: a+b,
'-': lambda a,b: a-b,
'*': lambda a,b: a*b,
'/': lambda a,b: a/b}
a, op, b = input('Enter equation: ').split()
print(operators[op](float(a), float(b))) lambda is used when you don't want a named function. I do not want my calculator program cluttered with named functions that all consist of one line. To use lamda as you do, to create a function when you don't need a function, is nonsensical. You could modify your code to make the lambda work, but what does this accomplish?
def power(number, exponent):
return (lambda a,b:a**b)(number, exponent)
def power(number, exponent):
return number**exponent
(Jul-03-2020, 02:39 PM)deanhystad Wrote: Using lambda functions for a calculator is common, but you are using them in the wrong place. A common way to use lambda functions for this task is to make a dictionary that associates a function to execute with an operator. Compare the two calculators below that are the same except one uses lambda to define the functions.
def subtract(a, b):
"""return a-b"""
return a-b
def addition(a, b):
"""return a+b"""
return a+b
def multiplication(a, b):
"""return a*b"""
return a*b
def division(a, b):
"""return a/b. Gotta make lint happy"""
return a/b
operators = {
'+': addition
'-': subtraction
'*': multiplication
'/': division
a, op, b = input('Enter equation: ').split()
print(operators[op](float(a), float(b))) operators = {
'+': lambda a,b: a+b,
'-': lambda a,b: a-b,
'*': lambda a,b: a*b,
'/': lambda a,b: a/b}
a, op, b = input('Enter equation: ').split()
print(operators[op](float(a), float(b))) lambda is used when you don't want a named function. I do not want my calculator program cluttered with named functions that all consist of one line. To use lamda as you do, to create a function when you don't need a function, is nonsensical. You could modify your code to make the lambda work, but what does this accomplish?
def power(number, exponent):
return (lambda a,b:a**b)(number, exponent)
def power(number, exponent):
return number**exponent
--------------------------------------------------------------------------------------
def subtract(a, b):
"""return a-b"""
return a-b
def addition(a, b):
"""return a+b"""
return a+b
def multiplication(a, b):
"""return a*b"""
return a*b
def division(a, b):
"""return a/b. Gotta make lint happy"""
return a/b
operators = {
'+': addition,
'-': subtract,
'*': multiplication,
'/': division}
a, op, b = input('Enter equation: ').split()
print(operators[op](float(a), float(b))) Output: a, op, b = input('Enter equation: ').split()
ValueError: not enough values to unpack (expected 3, got 1)
--------------------------
if i run above function as below equation , perfect run but when run without spaces then raise error as above described
Output: Enter equation: 3 - 1
2.0
Posts: 1,823
Threads: 2
Joined: Apr 2017
(Jul-03-2020, 07:55 PM)mbilalshafiq Wrote: if i run above function as below equation , perfect run but when run without spaces then raise error as above described
Of course it does, because split by default will split on whitespace and since there isn't any, it returns the whole string (i.e. a single item). If you don't specify a separator to split on, how do you expect it to know what to do?
|