Course Content

Python Exercise 2: Faulty Calculator Solution

In this tutorial, I have explained the Exercise-2 Solution. In Exercise-2, we need to create a faulty calculator. I am going to present the scenario and details related to the exercise here too.

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
#

print("Enter 1st Number")
num1 = int(input())
print('Enter 2nd Number')
num2 = int(input())
print('so What you Want?'+'+,-,/,%,*')
num3 =input()

if num1 ==45 and num2==3 and num3=='*':
    print("555")
elif num1 == 56 and num2 == 9 and num3 == '+':
        print("77")
elif num1 == 56 and num2 == 6 and num3 == '/':
        print("4")
elif num3=='*' :
    num4=num1*num2
    print(num4)
elif num3 == '+':
    plus=num2+num1
    print(plus)
elif num3 == '/':
    Dev=num2/num1
    print(Dev)
elif num3 == '-':
    Dev=num2-num1
    print(Dev)
elif num3 == '%':
    percent=num2%num1
    print(percent)
else:
    print("Error! Please check your input")

Comments(8)

SumitChauhan 1 year, 6 months ago
Its a code of previous video, kindly post the correct code
zadrain 1 year 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)


hirasingh 11 months, 4 weeks ago
Hi, I know nothing about computer languages and just started learning 4 days ago. This is the  very first program I have written with whatever limited knowledge I gained in the last 3-4 days. Please have a look and give your feedback.

# Faulty calculator
print("Plase enter the first number")
con_1: int = int(input())
print("Plese enter the second number")
con_2 = int(input())
print("Please select the operator")
con_3 = input()
if (con_1==56 and con_2==9 and con_3=="+") or (con_1==9 and con_2==56 and con_3=="+"):
    print(77)
elif con_3=="+":
    print(con_1+con_2)
elif (con_1==45 and con_2==3 and con_3=="*") or (con_1==3 and con_2==45 and con_3=="*"):
    print(555)
elif con_3=="*":
    print(con_1*con_2)
elif (con_1==56 and con_2==6 and con_3=="/") or (con_1==6 and con_2==56 and con_3=="/"):
    print(4)
elif con_3=="/":
    print(con_1/con_2)
elif con_3=="-":
    print(con_1-con_2)
else:
    print("Please entry correct data")
Vivek-Mishra 9 months, 3 weeks ago
Thanks, Harry Bhai for Awesome Tutorials |:::::::::::::::::|  Full Notes and Source Code :::::::::::::} https://github.com/Optimized-World/Complete-Python-Tutorial-and-Notes 
chaita 7 months, 3 weeks ago
print("                                  <<<<<<<<<<<<BRILLIANT CLACULATOR>>>>>>>>>>>")
var1 = int(input("enter the first number : "))
var2 = int(input("enter the second number : "))
var3 = input("enter the operation in alphabets like ADD, SUBTRACT : ")
if(var3.upper() == "ADD"):
    if (var1 == 56 and var2 == 9):
        print("77")
    else:
        print(var1+var2)
elif(var3.upper() == "SUBTRACT"):
    print(var1 - var2)
elif(var3.upper() == "DIVIDE"):
    if (var1 == 56 and var2 == 6):
        print("4")
    else:
        print(var1/var2)
elif(var3.upper() == "MULTIPLY"):
    if (var1 == 45 and var2 == 3):
        print("555")
    else:
        print(var1 * var2)

else:
    print("i can only perform multiplication, dividion, addition, subtraction")
saurabhad 5 months, 2 weeks ago
a=int(input("Enter the first number: "))
b=int(input("Enter the second number: "))
print("please select the choice:\n1.addition\n2.subtraction\n3.multiplication\n4.division\n")
c=int(input())
if (c==1):
    if (a==56 and b==9) or (a==9 and b==56):
        print("The addition: 77")
    else:
        print("The addition: ",a+b)
elif (c==2):
    print("The subtraction is: ",a-b)
elif (c==3):
    if (a==45 and b==3) or (a==3 and b==45):
        print("the multiplication is: 555")
saurabhad 5 months, 2 weeks ago
    else:
        print("The multiplications is: ",a*b)
elif (c==4):
    if (a==56 and b==6) or (b==56 and a==6):
        print("The division: 4")
    else:
        print("The division: ", a/b)
else:
    print("please select the valid number.")
karloz 2 months, 3 weeks ago
v1=float(input("Enter value 1 ="))

v2=float(input("Enter value 2 ="))
print('Enter following operator....+,*,/')
o=input("operator name=" )

if v1==56 and v2==9 and o=='+':
    print("77")
elif o=="+":
    print(v1+v2)
elif v1==45 and v2==3 and o=='*':
    print('555')
elif o=='*':
    print(v1*v2)
elif v1==56 and v2==6 and o=='/':
    print('4')
elif o=='/':
    print(v1/v2)
else:
    print('please check your operator')

Resources

  1. output.png - Download here

Course Announcements

Any Course related announcements will be posted here