Course Content

Python Exercise 2 - Faulty Calculator

Your task for today is “Faulty Calculator”. As the name gives away that, our program has something to do with a calculator.

In my upcoming exercises, you will notice that each one of them contains a scenario. The reason for that is just to develop your interest in solving the problems as it is very important for you to participate in these tasks as they will help you to develop logic making skills. Each of the scenarios will be totally unique and related to your interest as I know my audience. And also I have made each of the scenario myself. So, let me give a brief introduction of the scenario here.

Scenario:

Suppose that you are an invigilator in an exam. Calculator is not allowed for the exam. You discover somehow that students are planning to cheat in exam, using a calculator program in Python language. Somehow you get your hands on the calculator program and now you want to alter few results in calculator with your own provided results, so you can catch the students who are trying to cheat using the calculator program.

The user will provide the following inputs:

  • Operator
  • The two numbers, between which the operator is applied

All the results will be correct, except for these few:

  • 45 * 3 = 555
  • 56+9 = 77
  • 56/6 = 4

Code file as described in the video

# Exercise 2 - Faulty Calculator
# 45 * 3 = 555, 56+9 = 77, 56/6 = 4
# Design a calculator which will correctly solve all the problems except
# the following ones:
# 45 * 3 = 555, 56+9 = 77, 56/6 = 4
# Your program should take operator  and the two numbers as input from the user
# and then return the result

Comments(10)

mmishra 1 year, 5 months ago
print("Enter 1st number:",end=" ")
Number1 = int(input())
print("Enter 2nd number:",end=" ")
Number2 = int(input())
print("Enter operator +,-,*,/ on numbers:",end=" ")
Operator = input()
if (Number1 == 45 and Number2 == 3 or Number1 == 3 and Number2 == 45) and Operator == '*':
    print(Number1, Operator, Number2, " = ", 7777)
elif (Number1 == 56 and Number2 == 9 or Number1 == 9 and Number2 == 56) and Operator == '+':
    print(Number1, Operator, Number2, " = ", 77)
elif (Number1 == 56 and Number2 == 6 or Number1 == 6 and Number2 == 56) and Operator == '/':
    print(Number1, Operator, Number2, " = ", 4)
else:
    if Operator == '*':
        print(Number1, Operator, Number2, " = ",Number1*Number2)
    elif Operator == '+':
        print(Number1, Operator, Number2, " = ",Number1 + Number2)
    elif Operator == '-':
        print(Number1, Operator, Number2, " = ",Number1 - Number2)
    elif Operator == '/':
        print(Number1, Operator, Number2, " = ",Number1 / Number2)
    else:
        print("Unable to calculate, wrong operator")
thakurabhi 1 year, 1 month ago
print("Enter first number   ")
N1 = int(input())

print("Enter your operation + , - , / , * ")
op = (input())

print("Enter 2nd number   ")
N2 = int(input())



if (N1 == 45 and N2 == 3 or N1 == 3 and N2 == 45) and op == '*':
    print(N1,op,N2, " = " , 555)

elif (N1 == 56 and  N2 == 4 or N1== 4 and N2 == 56) and op == '+':
    print(N1,op,N2, " = " , 77)
    
elif (N1 == 56 and N2 == 6 or N1 == 6 and N2 == 5656) and op == '/':
    print(N1,op,N2, " = " , 4)
    
else:
    if op == '+':
        print(N1,op,N2, " = ", N1 + N2)
        
    elif op == '*':
        print(N1,op,N2, " = ", N1 * N2)
        
    elif op == '/':
        print(N1,op,N2, " = ", N1 / N2)
        
    elif op == '-':
        print(N1,op,N2, " = ", N1 - N2)
        
    else:
        print("over")
    
Manav_Seng 1 year ago
Name: Manav Sengupta

Solution:

# CodewithHarry Python Excercise 2- Faulty Calculator
Number_1=float(input("Enter the First Number"))
Number_2=float(input([float(input("Enter the Second Number"))]))
operator = input("Enter 'add' for Addition ; \nEnter'sub' for Subtraction ; \nEnter 'mul' for Multiplication ; \nEnter 'div' for Division")
if ((Number_1 == 45 and Number_2 == 3 and operator=='mul') or (Number_1 == 56 and Number_2 == 6 and operator=='div') or (Number_1 == 56 and Number_2 == 9 and operator=='add')):
    if (Number_1 == 45):
        print("The Answer is : 555")
    elif (Number_2 == 9):
        print("The Answer is : 74")
    else:
        print("The Answer is 4")
else:
    if(operator=='add'):
        Answer=Number_1+Number_2
    elif(operator=='sub'):
        Answer=Number_1-Number_2
    elif(operator=='mul'):
        Answer=Number_1*Number_2
    elif(operator=='div'):
        Answer=Number_1/Number_2
    else:
        Answer="Sorry Invalid Input"
    print("The Answer is : "+Answer)

print(147*"*"+"\n*"+145*" "+"*\n*"+9*" "+"Thanks for checking out my code, and a ton of thanks to CodewithHarry Channel for all the Quality Content for Free"+22*" "+"*\n*"+145*" "+"*\n"+147*"*") # Gratitude
Manav_Seng 1 year ago
sorry i screwed  it.....
here is the correct one:

# CodewithHarry Python Excercise 2- Faulty Calculator
Number_1=float(input("Enter the First Number"))
Number_2=float(input("Enter the Second Number"))
operators = input("Enter 'add' for Addition ; \nEnter'sub' for Subtraction ; \nEnter 'mul' for Multiplication ; \nEnter 'div' for Division")
if ((Number_1 == 45 and Number_2 == 3 and operators=='mul') or (Number_1 == 56 and Number_2 == 6 and operators=='div') or (Number_1 == 56 and Number_2 == 9 and operators=='add')):
    if (Number_1 == 45):
        print("The Answer is : 555")
    elif (Number_2 == 9):
        print("The Answer is : 74")
    else:
        print("The Answer is 4")
else:
    if(operators=='add'):
        Answer=Number_1+Number_2
    elif(operators=='sub'):
        Answer=Number_1-Number_2
    elif(operators=='mul'):
        Answer=Number_1*Number_2
    elif(operators=='div'):
        Answer=Number_1/Number_2
    else:
        Answer="Sorry Invalid Input"
    print("The Answer is : "+str(Answer))

print(147*"*"+"\n*"+145*" "+"*\n*"+9*" "+"Thanks for checking out my code, and a ton of thanks to CodewithHarry Channel for all the Quality Content for Free"+22*" "+"*\n*"+145*" "+"*\n"+147*"*") # Gratitude
zadrain 11 months, 1 week ago
#45*3=555
#56+9 = 77
#56/6 =4
first_number = int(input("1st number"))
second_number = int(input("2nd number"))
operator = (input("choose operator"))
if first_number ==45 and second_number==3 and operator=="*":
    print("555")
elif first_number ==56 and second_number==9 and operator=="+":
    print("77")
elif first_number ==56 and second_number==6 and operator=="/":
    print("4")
else:
    if operator=="+":
        print(first_number + second_number)
    elif operator=="*":
        print(first_number * second_number)
    elif operator=="/":
        print(first_number / second_number)
    elif operator=="-":
        print(first_number - second_number)


AR78692 10 months, 2 weeks ago
put1=int(input('Enter your first number:'))
operator=input('Enter your operatior + - / *:')
put2=int(input('Enter your second number:'))
#for addition
if operator=='+':
    if put1==56 and put2==9:
        put3=77
        print('your total add is:',put3)
    else:
        print('Your total add is:',put1+put2)
#for subtraction
if operator=='*':
    if put1==45 and put2==3:
        put3=555
        print('Your total multipluction is:',put3)
    else:
        print('your total multipluction is:',put1*put2)
#for division
if operator=='/':
    if put1==56 and put2==6:
        put3=4
        print('Your division is:',put3)
    else:
        print('your total division is:',put1/put2)




GaziAbbas 9 months, 3 weeks ago
# Exercise 2 - Faulty Calculator
# 45 * 3 = 555, 56+9 = 77, 56/6 = 4
# Design a calculator which will correctly solve all the problems except
# the following ones:
# 45 * 3 = 555, 56+9 = 77, 56/6 = 4
# Your program should take operator  and the two numbers as input from the user
# and then return the result

print("Note: Please Enter Only (+,-,/,*) Operator Only\n Please Input Only Non-Decimal Value")
oprtr = input("Enter Operator: ")
num1 = int(input("Enter First Number: "))
num2 = int(input("Enter Second Number: "))

ans = 0
oprtionPerofomed = True

if oprtr == '+':
    if ((num1 == 56 and num2 == 9) or (num1 == 9 and num2 == 56)):
       ans = 77
    else:
        ans = num1 + num2
elif oprtr == '-':
    ans = num1 - num2
elif oprtr == '*':
    if ((num1 == 45 and num2 == 3) or (num1 == 3 and num2 == 45)):
       ans = 555
    else:
        ans = num1 * num2
elif oprtr == '/':
    if ((num1 == 56 and num2 == 6) or (num1 == 6 and num2 == 56) ):
       ans = 4
    else:
        ans = num1 / num2
else:
    print("Please Select Proper Operator")
    oprtionPerofomed = False

if oprtionPerofomed:
    print(" ", num1, " ", oprtr, " ", num2, " = ", ans)
sushil2002 9 months, 1 week ago
# Exercise 2: faulty calci
#*+/
num1=int(input("enter a number "))
op=input("enter operator(+,-,*,/")
if op== "+":
    num2 = int(input("enter another number to perform operation"))
    print("result=",555)
elif op=="*":
    num2 = int(input("enter another number to perform operation"))
    print("result=",386)
elif op=="/":
    num2 = int(input("enter another number to perform operation"))
    print("result=",54)
elif op=="-":
    num2=int(input("enter another number to perform operation"))
    print("result=",num1-num2)
else:
    print("invalid operator")
Vivek-Mishra 8 months, 1 week ago
Full Notes and Source Code ::::::::::} https://github.com/Optimized-World/Complete-Python-Tutorial-and-Notes |::::::::::::| Thanks, Harry Bhai for Awesome Tutorials
pranay94 8 months, 1 week ago
print("Enter the operator you want to perform on the calculator: ")
inp1 = input()
print("Enter the first number: ")
inp2 = int(input())
print("Enter the second number: ")
inp3 = int(input())

if inp1=="+":
    print("Addition of number is ", inp2-inp3)
elif inp1=="*":
    print("Multiplcation of number is ", inp2/inp3)
elif inp1=="-":
    print("Substraction of number is ", inp2+inp3)
elif inp1=="/":
    print("Division of number is ", inp2*inp3)
else:
    print("Ooopps!! Sorry")
shashankt 8 months, 1 week ago
Bro Well done. But you have to add code for wrong statements as well which Harry bhai mentioned in the Video.
shashankt 8 months, 1 week ago
But Correct Code is :   

calc = ["+", "-", "*", "/"]
num1 = int(input("Enter the First Number: "))
calc = input("Enter the Operator: ")
num2 = int(input("Enter the Second Number: "))
if calc == "+":
    if (num1 == 56 and num2 == 9):
        print(77)
        exit()
    print(num1 + num2)
elif calc == "-":
    if (num1 == 56 and num2 == 9):
        print(88)
        exit()
    print(num1 - num2)
elif calc == "*":
    if (num1 == 45 and num2 == 3):
        print(555)
        exit()
    print(num1 * num2)
elif calc == "/":
    if (num1 == 56 and num2 == 6):
        print(4)
        exit()
    print(num1 / num2)
else:
    print("Thanks for Using Calculator")

Resources

No downloadable resources for this video. If you think you need anything, please post it in the QnA!

Course Announcements

Any Course related announcements will be posted here