0

I wrote a Python programm that is ignoring the if loop. Could somebody please help me by telling me what I did wrong? Please see the code below:

class Check(): def __init__(self, op1, op2, op): self.op1 = op1 self.op2 = op2 self.op = op def main(): print("Enter first number: ") num1 = int(input()) print("Enter second number: ") num2 = int(input()) print("Enter operation: ") op=str(input()) if op == '+': return op1 + op2 elif op == '-': return op1 - op2 elif op == '*': return op1 * op2 elif op == '/': return op1 / op2 if __name__ == "__main__": main() 

2
  • 4
    Well, first of all if is not a loop. Second, your code just defines a class but no instance or method are ever created/called Commented Aug 5, 2018 at 23:09
  • Where is your main driver? What's the input the user gives it? Commented Aug 5, 2018 at 23:09

1 Answer 1

1

The problem is there is nothing that is op1 and op2, there is self.op1, self.op2, num1, and num2. I am assuming you want to add num1 and num2. Also you didn't put

self 

in cheese, here is the working program

class Check(): def __init__(self, op1, op2, op): self.op1 = op1 self.op2 = op2 self.op = op def cheese(self): print("Enter first number: ") op1 = int(input()) print("Enter second number: ") op2 = int(input()) print("Enter operation: ") op=str(input()) if op == '+': return op1 + op2 elif op == '-': return op1 - op2 elif op == '*': return op1 * op2 elif op == '/': return op1 / op2 a = Check(1,2,"+") print(a.cheese()) 
Sign up to request clarification or add additional context in comments.

2 Comments

Hello the code was edited to include a main() function. I now get an error that main() is not defined.
Again you need to put self, as its part of a class, then you have to initialize Check with something like 'a = Check()' then because main is part of the Check class you would say 'a.main()' it might not make sense to you now, but i'm sure in a while you will understand it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.