Skip to content

Instantly share code, notes, and snippets.

@mjcarnaje
Created June 3, 2022 02:13
Show Gist options
  • Save mjcarnaje/e111ee399cba7fcad1de0a256f5aa4b8 to your computer and use it in GitHub Desktop.
Save mjcarnaje/e111ee399cba7fcad1de0a256f5aa4b8 to your computer and use it in GitHub Desktop.
Python
def main():
books = [
{
'title': 'The Giver',
'author': 'Lois Lowry',
'isBorrowed': False
},
{
'title': 'The Hunger Games',
'author': 'Suzanne Collins',
'isBorrowed': False
},
{
'title': 'The Hobbit',
'author': 'J.R.R. Tolkien',
'isBorrowed': False
},
{
'title': 'The Chronicles of Narnia',
'author': 'C.S. Lewis',
'isBorrowed': False
},
{
'title': 'The Lion, the Witch and the Wardrobe',
'author': 'C.S. Lewis',
'isBorrowed': False
},
{
'title': 'The Da Vinci Code',
'author': 'Dan Brown',
'isBorrowed': False
},
{
'title': 'The Alchemist',
'author': 'Paulo Coelho',
'isBorrowed': False
},
]
print("Welcome to the book rental system")
print("--------------------------------")
def display_books():
print("\n")
print("Available books are: ")
print("----------------------")
for book in books:
if book['isBorrowed'] == False:
print("Title: " + book['title'])
print("Author: " + book['author'])
print("----------------------")
def borrow_book():
print("\n")
print("Enter the name of the book you want to borrow: ")
book_name = input("Enter the name of the book: ")
is_book_found = False
for book in books:
if book['title'] == book_name:
is_book_found = True
if book['isBorrowed'] == False:
book['isBorrowed'] = True
print("Book borrowed")
break
else:
print("Book is already borrowed")
break
if is_book_found == False:
print("Book not found")
def return_book():
print("\n")
print("Enter the name of the book you want to return: ")
book_name = input("Enter the name of the book: ")
is_book_found = False
for book in books:
if book['title'] == book_name:
is_book_found = True
if book['isBorrowed'] == True:
book['isBorrowed'] = False
print("Book returned")
break
else:
print("Book is not borrowed")
break
if is_book_found == False:
print("Book not found")
while True:
print("\n")
print("Enter 1: To Display available books")
print("Enter 2: To Borrow a book")
print("Enter 3: To Return a book")
print("Enter 4: To exit")
print("Select a choice from 1-4")
choice = int(input("Enter your choice: "))
if choice == 1:
display_books()
elif choice == 2:
borrow_book()
elif choice == 3:
return_book()
elif choice == 4:
exit()
main()
# Student Information System (CRUD)
def main():
students = [
{
"name": "John",
"sr_code": "123",
"age": 25,
"major": "Computer Science",
"email_id": "john@gmail.com",
"address": "123 Main St",
}
]
def add_student():
print("Adding a new student:\n")
name = input("Enter the name of the student: ")
sr_code = input("Enter the student's SR Code: ")
age = int(input("Enter the student's age: "))
major = input("Enter the student's major: ")
email_id = input("Enter the student's email id: ")
address = input("Enter the student's address: ")
students.append({
"name": name,
"sr_code": sr_code,
"age": age,
"major": major,
"email_id": email_id,
"address": address,
})
print("\nStudent added successfully!")
def update_student():
print("Updating a student:\n")
sr_code = input("Enter the SR of the student: ")
is_student_found = False
for student in students:
if student['sr_code'] == sr_code:
is_student_found = True
student['name'] = input("Enter the new name of the student: ")
student['age'] = int(input("Enter the new student's age: "))
student['major'] = input("Enter the new student's major: ")
student['email_id'] = input(
"Enter the new student's email id: ")
student['address'] = input("Enter the new student's address: ")
print("\nStudent updated successfully!")
break
if is_student_found == False:
print("\nStudent not found")
def delete_student():
print("Deleting a student:\n")
sr_code = input("Enter the SR of the student: ")
is_student_found = False
for student in students:
if student['sr_code'] == sr_code:
is_student_found = True
students.remove(student)
break
if is_student_found == False:
print("\nStudent not found")
def display_students():
print("Displaying all students:\n")
if(len(students) == 0):
print("\nNo students found")
else:
print("\nStudents: ")
print("----------------------")
for student in students:
print("Name: " + student['name'])
print("SR Code: " + student['sr_code'])
print("Age: " + str(student['age']))
print("Major: " + student['major'])
print("Email ID: " + student['email_id'])
print("Address: " + student['address'])
print("----------------------\n")
def get_student_by_sr():
print("Getting a student by SR Code:\n")
sr_code = input("Enter the SR of the student: ")
is_student_found = False
for student in students:
if student['sr_code'] == sr_code:
is_student_found = True
print("\nStudent Information: ")
print("----------------------")
print("Name: " + student['name'])
print("SR Code: " + student['sr_code'])
print("Age: " + str(student['age']))
print("Major: " + student['major'])
print("Email ID: " + student['email_id'])
print("Address: " + student['address'])
print("----------------------\n")
break
if is_student_found == False:
print("\nNo students found")
print("Welcome to the student information system")
while True:
print("-----------MENU-----------")
print("1. Add student")
print("2. Update student")
print("3. Delete student")
print("4. Display students")
print("5. Display specific student")
print("6. Exit")
choice = int(input("Enter your choice: "))
print("--------------------------")
if choice == 1:
add_student()
elif choice == 2:
update_student()
elif choice == 3:
delete_student()
elif choice == 4:
display_students()
elif choice == 5:
get_student_by_sr()
elif choice == 6:
break
else:
print("Invalid choice")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment