1. Write a menu drive program to perform following operations into a binary file shoes.dat. 8
a) Add record b) Display records c) Search record d) Exit The structure of file content is: [s_id, name, brand, type, price]Tuesday, January 30, 2024
CLASS XII: PRACTICAL PROGRAMS
Solution:
import pickle
def add_record():
s_id = int(input("Enter Shoe ID: "))
name = input("Enter Name: ")
brand = input("Enter Brand: ")
shoe_type = input("Enter Type: ")
price = float(input("Enter Price: "))
record = (s_id, name, brand, shoe_type, price)
with open("shoes.dat", "ab") as file:
pickle.dump(record, file)
print("Record added successfully.")
def display_records():
try:
with open("shoes.dat", "rb") as file:
while True:
record = pickle.load(file)
print("Shoe ID:", record[0])
print("Name:", record[1])
print("Brand:", record[2])
print("Type:", record[3])
print("Price:", record[4])
print("-" * 30)
except EOFError:
pass
except FileNotFoundError:
print("File not found. No records to display.")
def search_record():
try:
s_id = int(input("Enter Shoe ID to search: "))
with open("shoes.dat", "rb") as file:
while True:
record = pickle.load(file)
if record[0] == s_id:
print("Record found:")
print("Shoe ID:", record[0])
print("Name:", record[1])
print("Brand:", record[2])
print("Type:", record[3])
print("Price:", record[4])
return
except EOFError:
pass
except FileNotFoundError:
print("File not found. No records to search.")
def main():
while True:
print("\nMenu:")
print("a) Add record")
print("b) Display records")
print("c) Search record")
print("d) Exit")
choice = input("Enter your choice: ").lower()
if choice == 'a':
add_record()
elif choice == 'b':
display_records()
elif choice == 'c':
search_record()
elif choice == 'd':
print("Exiting program. Goodbye!")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
2.
Solution:
import mysql.connector as mycon
# i. Fill in the parameters and values for statement 1
cn = mycon.connect(user='root', password='tiger', host='localhost',database='Customer')
# ii. Write function name to create a cursor and fill in the gap for statement 2
cr = cn.cursor()
cust_id = int(input("Enter ID:"))
cust_name = input("Enter Customer Name:")
city = input("Enter City:")
ba = float(input("Enter Bill Amount:"))
mno = input("Enter Mobile No.")
# iii. Write a query to fill statement 3 with desired values
query = "INSERT INTO customer (CustomerID, CustomerName, City, BillAmt, MobileNo) VALUES (%s, %s, %s, %s, %s)"
values = (cust_id, cust_name, city, ba, mno)
cr.execute(query, values)
# iv. Write a query to fill statement 4 to save the records into the table
cn.commit()
# Close the cursor and connection
cr.close()
cn.close()
Saturday, January 27, 2024
CLASS XI: IMPORTANT PROGRAMS OF PYTHON
1. Write a program to check if all the elements of a tuple are in descending order or not
# function to check if tuple is sorted in descending order
def is_tuple_sorted(t):
for i in range(1, len(t)):
# return False if the element is greater than the previous element
if t[i] > t[i-1]:
return False
return True
# create a tuple
t = (5, 4, 3, 2, 1)
# check if tuple is sorted
2. # Python program to check if the number is an Armstrong number or not
# take input from the user
num = int(input("Enter a number: "))
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
# display the result
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
Output 1
Enter a number: 663 663 is not an Armstrong number
Output 2
Enter a number: 407 407 is an Armstrong number
Here, we ask the user for a number and check if it is an Armstrong number.
We need to calculate the sum of the cube of each digit. So, we initialize the sum to 0 and obtain each digit number by using the modulus operator %. The remainder of a number when it is divided by 10 is the last digit of that number. We take the cubes using exponent operator.
Finally, we compare the sum with the original number and conclude that it is Armstrong number if they are equal.
Source Code: Check Armstrong number of n digits
num = 1634
# Changed num variable to string,
# and calculated the length (number of digits)
order = len(str(num))
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10
# display the result
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
You can change the value of num in the source code and run again to test it.
Subscribe to:
Posts (Atom)