Lesson 12 Logic Programming
Lesson 12 Logic Programming
Logic Programming uses facts and rules for solving the problem. That is why they are
called the building blocks of Logic Programming. A goal needs to be specified for every
program in logic programming. To understand how a problem can be solved in logic
programming, we need to know about the building blocks − Facts and Rules −
Facts
Actually, every logic program needs facts to work with so that it can achieve the given
goal. Facts basically are true statements about the program and data. For example,
Delhi is the capital of India.
Rules
Actually, rules are the constraints which allow us to make conclusions about the
problem domain. Rules basically written as logical clauses to express various facts. For
example, if we are building any game then all the rules must be defined.
Rules are very important to solve any problem in Logic Programming. Rules are
basically logical conclusion which can express the facts. Following is the syntax of rule
−
A∶− B1,B2,...,Bn.
Here, A is the head and B1, B2, ... Bn is the body.
For example − ancestor(X,Y) :- father(X,Y).
ancestor(X,Z) :- father(X,Y), ancestor(Y,Z).
This can be read as, for every X and Y, if X is the father of Y and Y is an ancestor of Z,
X is the ancestor of Z. For every X and Y, X is the ancestor of Z, if X is the father of Y
and Y is an ancestor of Z.
Program or Solution
n = int(input())
count = 0
while n > 0:
n = n // 10
count = count + 1
print(count)
Program Explanation
if we divide a number by 10, then last digit of the same number will removed and we get
remaining digits as output. Do the same till number becomes 0 and count iteration. So the
count is number of digits of the given number .
Program or Solution
Program or Solution
Program Explanation
initialize total is equal to 0
calculate the remainder of a number by doing number % 10, add the remainder to total
and divide the number by the 10 and repeat the above steps till number becomes zero.
Print the total, you will get the sum of digits of the number
Output
Program Explanation
initialize total is equal to 0
calculate the remainder of a number by doing number % 10, add the remainder to (total *
10) and divide the number by the 10 and repeat the above steps till number becomes zero.
Print the total, you will get the sum of digits of the number.
Program or Solution
#hashed lines are comment lines used to illustrate the program
secret_number = randint(1,5)
if secret_number == guess_number:
else:
Program Explanation
Line 3: Imports the randint function from the package random
Line 5: using randint system generates a secret number
Line 7 : getting a guess from user
line 9 : check whether secret number generated by system and guessing number entered
by user are same
line 10 : if same, display "your guess is correct"
line 12 : if not same, display "your guess is wrong"
At each stage the points will be awarded to user based on following condition
Solved in points
first attempt 100
second attempt 75
third attempt 50
fourth attempt 25
fifth attempt 10
Program or Solution
level = 1
points = [100,75,50,25,10]
end_value = level * 10
secret_number = randint(1,end_value)
if secret_number == guess_number:
points_scored.append(points[counter])
level = level + 1
break
else:
else:
break
else:
print("Congratz, You Won the Game with
{} !!!".format(sum(points_scored)))
for i in range(0,3):
Program Explanation
Logic is same as level 4, in addition,
points are stored using list
points_scored variable is a list which stores the points scored by user at each stage
the final for loop shows the points scored by user at each stage.
Program or Solution
for i in range(2,n//2+1):
if n % i == 0: #if divisible then it is not prime.
break
else: #this is else of for statement. executes after last iteration if
loop is not broken at any iteration.
print("{} ".format(n))
Program Explanation
Prime numbers are numbers those only divisible by 1 and same. using a outer for loop we
check take every number n from x to y
Inner for loop checks each number is prime or not, if prime it prints the n.
102:{'item_name':'Banana','Quantity_in_kgs':30,'Price_per_kg':80},
103:{'item_name':'Grapes','Quantity_in_kgs':25,'Price_per_kg':300},
104:{'item_name':'Lemon','Quantity_in_kgs':20,'Price_per_kg':70}
}
Code the following tasks to implement Billing System for a Fruit Shop.
3. Check Stock
a. Display the items and available quantity
b. Highlight the items which are available less than 5 kgs.
Program or Solution
items = {
101:
{'item_name':'Apple','Quantity_in_kgs':50,'Price_per_kg':200},
102:
{'item_name':'Banana','Quantity_in_kgs':30,'Price_per_kg':80},
103:
{'item_name':'Grapes','Quantity_in_kgs':25,'Price_per_kg':300},
104:
{'item_name':'Lemon','Quantity_in_kgs':20,'Price_per_kg':70}
}
trans = {}
#Stock Printing
def stock_check():
print("Item Name | Available Stock")
for item_id in items:
if items[item_id]['Quantity_in_kgs'] <= 5:
print("----------------------------------------")
print("| ",items[item_id]['item_name'], " | ",items[item_id]
['Quantity_in_kgs']," |")
print("----------------------------------------")
else:
print("| ",items[item_id]['item_name'], " | ",items[item_id]
['Quantity_in_kgs']," |")
#Add New Item to Database
def add_new_item():
item_ids = list(items.keys())
item_id = max(item_ids) + 1
item_name = input("Enter Item Name:")
price = int(input("Enter Price Per Kilo Gram:"))
quantiy = int(input("Enter Quantity"))
item =
{'item_name':item_name,'Quantity_in_kgs':quantiy,'Price_per_kg':price}
items[item_id]= item
print('Item Added')
print('Item id | Item Name | Quantity | Price ')
print(item_id, " | ", item_name, " | ", quantiy, " | ", price)
#Update the Quantity of Existing Item
def update_item(item_id):
quantiy = int(input("Enter Quantity"))
items[item_id]['Quantity_in_kgs'] = items[item_id]['Quantity_in_kgs'] +
quantiy
print('Item Updated')
print('Item id | Item Name | Quantity | Price ')
print(item_id, " | ", items[item_id]['item_name'], " | ", items[item_id]
['Quantity_in_kgs'], " | ", items[item_id]['Price_per_kg'] )
#Stock Entry
def add_item():
item_name = input("Enter the Item Name")
print("item id | item name")
print("-----------------")
for item in items:
if item_name in items[item]['item_name']:
print(item, " | ", items[item]['item_name'])
Program Explanation
add_item() method checks whether the item is existing item or new item to the fruit shop.
if item is new then it calls add_new_item() method else it call update_item() method.
sale() method gets detail of each item and fetch price from dictionary to calculate amount.
amount will be added to the total. Sale() method finally decrease the sold quantity in item
dictionary.
stock_check() method prints all items and its available quantity.