mangafas - 6 months ago 14

Python Question

So I have been trying to create calculator in Python and so far I've been good but now I got a major promblem and I don't know what to do. I know that the code could have been more simple but I will need it as it it for the future.

`class Calculator:`

def addition(self):

try:

n = int(input("enter number: ")) # user enters the n value

n_for_add = int(input("What do you want to add on " + str(n) + " ? ")) # user enters the n_for_add value

except ValueError: # if the value is not an integer it will raise an error

print("you must enter an integer!") # and print you this. This will automatically kill the program

n_from_add = n + n_for_add

print(str(n) + " plus " + str(n_for_add) + " equals to " + str(n_from_add)) # this will print a nice output

def subtraction(self):

try:

nu = int(input("enter number: "))

nu_for_sub = int(input("What do you want to take off " + str(nu) + " ? "))

except ValueError:

print("you must enter an integer!")

nu_from_sub = nu - nu_for_sub

print(str(nu) + " minus " + str(nu_for_sub) + " equals to " + str(nu_from_sub))

# this is the same as addition but it subtracts instead of adding

def division(self):

try:

num = int(input("enter number: "))

num_for_div = int(input("What do you want to divide " + str(num) + " off? "))

except ValueError:

print("you must enter an integer!")

num_from_div = num / num_for_div

print(str(num) + " divided by " + str(num_for_div) + " equals to " + str(num_from_div))

# same as others but with division this time

def Multiplication(self):

try:

numb = int(input("enter number: "))

numb_for_multi = int(input("What do you want to multiply " + str(numb) + " on? "))

except ValueError:

print("you must enter an integer!")

numb_from_multi = numb * numb_for_multi

print(str(numb) + " multiplied by " + str(numb_for_multi) + " equals to " + str(numb_from_multi))

# its the same as others but with multiplication function

def choice(self):

print("A. Addition\l B. Substraction\l C. Division\l D. Multiplication")

xn = input("What do you want to do? ")

if xn == "a":

x = self.addition()

self.x = x

return x

elif xn == "b":

z = self.subtraction()

self.z = z

return z

elif xn == "c":

y = self.division()

self.y = y

return y

elif xn == 'd':

v = self.Multiplication()

self.v = v

return v

objectCalculator = Calculator()

print(objectCalculator.choice())

Answer

The problem is in the indent in your `if`

statements in `choice`

.
The function should be re-written like this:

```
def choice(self):
print("A. Addition\l B. Substraction\l C. Division\l D. Multiplication")
xn = input("What do you want to do? ")
if xn == "a":
x = self.addition()
self.x = x
return x
elif xn == "b":
z = self.subtraction()
self.z = z
return z
elif xn == "c":
y = self.division()
self.y = y
return y
elif xn == 'd':
v = self.Multiplication()
self.v = v
return v
```

Python is unlike many other languages in that it does not use braces to enclose control structures. It only looks at how many spaces away from the beginning of the line to determine if a statement is within the structure. This goes for `if`

, `elif`

, `else`

, `for`

, `while`

, `def`

, `class`

and more.

You can find out more about that from the docs or from here.