+ 1

Help me with my code.

Hey guys. I was trying to code an easy calculator but it always errors. Can someone help me?

25th Dec 2016, 6:39 PM
Ali Emir Kızıl
Ali Emir Kızıl - avatar
10 Answers
+ 3
what did you need it to do?
25th Dec 2016, 6:59 PM
Ian Croasdell
Ian Croasdell - avatar
+ 3
<?php $result = ""; class calculator { var $a; var $b; function checkopration($oprator) { switch($oprator) { case '+': return $this->a + $this->b; break; case '-': return $this->a - $this->b; break; case '*': return $this->a * $this->b; break; case '/': return $this->a / $this->b; break; default: return "Sorry No command found"; } } function getresult($a, $b, $c) { $this->a = $a; $this->b = $b; return $this->checkopration($c); } } $cal = new calculator(); if(isset($_POST['submit'])) { $result = $cal->getresult($_POST['n1'],$_POST['n2'],$_POST['op']); } ?> <form method="post"> <table align="center"> <tr> <td><strong><?php echo $result; ?><strong></td> </tr> <tr> <td>Enter 1st Number</td> <td><input type="text" name="n1"></td> </tr> <tr> <td>Enter 2nd Number</td> <td><input type="text" name="n2"></td> </tr> <tr> <td>Select Oprator</td> <td><select name="op"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select></td> </tr> <tr> <td></td> <td><input type="submit" name="submit" value=" = "></td> </tr> </table> </form>
25th Dec 2016, 7:22 PM
Ian Croasdell
Ian Croasdell - avatar
+ 3
# define functions def add(x, y): return x + y def subtract(x, y): return x - y def multiply(x, y): return x * y def divide(x, y): return x / y # take input from the user print("Select operation.") print("1.Add") print("2.Subtract") print("3.Multiply") print("4.Divide") choice = input("Enter choice(1/2/3/4):") num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) if choice == '1': print(num1,"+",num2,"=", add(num1,num2)) elif choice == '2': print(num1,"-",num2,"=", subtract(num1,num2)) elif choice == '3': print(num1,"*",num2,"=", multiply(num1,num2)) elif choice == '4': print(num1,"/",num2,"=", divide(num1,num2)) else: print("Invalid input") __________________________ you can do it like this print("Select operation.") print("1.Add") print("2.Subtract") print("3.Multiply") print("4.Divide") choice = input("Enter choice(1/2/3/4):") num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) if choice == '1': print(num1,"+",num2,"=", num1+num2) elif choice == '2': print(num1,"-",num2,"=", num1-num2) elif choice == '3': print(num1,"*",num2,"=", num1*num2) elif choice == '4': print(num1,"/",num2,"=", num1/num2) else: print("Invalid input")
25th Dec 2016, 11:45 PM
Minovsky
Minovsky - avatar
+ 2
A calculator. I'm a beginner, it was going to be something easy
25th Dec 2016, 7:13 PM
Ali Emir Kızıl
Ali Emir Kızıl - avatar
+ 2
But input code created errors
25th Dec 2016, 7:13 PM
Ali Emir Kızıl
Ali Emir Kızıl - avatar
+ 2
Thank you!
25th Dec 2016, 7:54 PM
Ali Emir Kızıl
Ali Emir Kızıl - avatar
+ 1
Fill in the blanks to declare a function myFunction, taking two parameters and printing the product of their multiplication to the screen. myFunction($a, $b) { echo $a $b; }
24th Dec 2019, 4:56 AM
[No Name]
[No Name] - avatar
0
what the fuck is the number
29th Mar 2019, 12:56 PM
Bradley Beacock
Bradley Beacock - avatar
0
guys help me min(x, y): if x<=y return x else: y
23rd Jul 2019, 11:08 AM
Arjun Acharya
Arjun Acharya - avatar
0
Fill in the blanks to declare a function myFunction, taking two parameters and printing the product of their multiplication to the screen. function myFunction($a, $b) { echo $a * $b; }
4th Apr 2022, 3:16 PM
EL AZZOUZI NORDINE
EL AZZOUZI NORDINE - avatar