Ads

AI lab Assignment next 2 questions

 Q4)

1.     Define a library database as a dictionary where the keys are book titles and the values are tuples containing book information such as author, genre, publication year, and availability status. Initialize the database with at least five books.

Implement the following functionalities:

         1.1.                  Add a new book to the library.

         1.2.                  Remove a book from the library.

         1.3.                  Update a book's information.

         1.4.                  Display all books' information.

         1.5.                  Search for books by title, author, or genre.

         1.6.                  Allow users to borrow and return books.

2.     Ensure user-friendly interactions. Display appropriate messages for each action.

3.     Implement a basic user authentication system where users have to enter a username and password to access the library functionalities.

4.     Use loops (for/while), conditional statements (if/elif/else), and nested structures as necessary to implement the functionalities.

5.     Test your program with various scenarios to ensure it handles different cases correctly.

# Simple login system

username = input("Enter username: ")

password = input("Enter password: ")


if username != "admin" or password != "1234":

    print("Access denied. Invalid credentials.")

else:

    print("Access granted. Welcome to the Library System!")


    # Initial library database: title -> (author, genre, year, availability)

    library = {

        "1984": ("George Orwell", "Dystopian", 1949, True),

        "To Kill a Mockingbird": ("Harper Lee", "Classic", 1960, True),

        "The Great Gatsby": ("F. Scott Fitzgerald", "Classic", 1925, True),

        "Brave New World": ("Aldous Huxley", "Science Fiction", 1932, True),

        "Python Basics": ("John Doe", "Programming", 2020, True)

    }


    while True:

        print("\nLibrary Menu:")

        print("1. Add a new book")

        print("2. Remove a book")

        print("3. Update book info")

        print("4. Display all books")

        print("5. Search books")

        print("6. Borrow a book")

        print("7. Return a book")

        print("8. Exit")


        choice = input("Enter your choice (1-8): ")


        if choice == "1":

            title = input("Enter book title: ")

            author = input("Enter author: ")

            genre = input("Enter genre: ")

            year = int(input("Enter publication year: "))

            library[title] = (author, genre, year, True)

            print("Book added.")


        elif choice == "2":

            title = input("Enter title of the book to remove: ")

            if title in library:

                del library[title]

                print("Book removed.")

            else:

                print("Book not found.")


        elif choice == "3":

            title = input("Enter title of the book to update: ")

            if title in library:

                author = input("Enter new author: ")

                genre = input("Enter new genre: ")

                year = int(input("Enter new publication year: "))

                available = library[title][3]

                library[title] = (author, genre, year, available)

                print("Book info updated.")

            else:

                print("Book not found.")


        elif choice == "4":

            print("\nAll books:")

            for title, info in library.items():

                status = "Available" if info[3] else "Borrowed"

                print(f"Title: {title} | Author: {info[0]} | Genre: {info[1]} | Year: {info[2]} | Status: {status}")


        elif choice == "5":

            search = input("Search by title/author/genre: ").lower()

            found = False

            for title, info in library.items():

                if (search in title.lower() or 

                    search in info[0].lower() or 

                    search in info[1].lower()):

                    print(f"Found: {title} by {info[0]} | Genre: {info[1]} | Year: {info[2]} | {'Available' if info[3] else 'Borrowed'}")

                    found = True

            if not found:

                print("No books matched your search.")


        elif choice == "6":

            title = input("Enter title of the book to borrow: ")

            if title in library:

                if library[title][3]:

                    info = library[title]

                    library[title] = (info[0], info[1], info[2], False)

                    print("Book borrowed.")

                else:

                    print("Book is already borrowed.")

            else:

                print("Book not found.")


        elif choice == "7":

            title = input("Enter title of the book to return: ")

            if title in library:

                info = library[title]

                library[title] = (info[0], info[1], info[2], True)

                print("Book returned.")

            else:

                print("Book not found.")


        elif choice == "8":

            print("Exiting the program. Goodbye!")

            break


        else:

            print("Invalid choice. Please enter a number from 1 to 8.")



Q5: Instructions:

PLO

CLO

Learning

Level/LL

5

1

P-3

 

The local driver’s license office has asked you to create an application that grades the written portion of the driver’s license exam. The exam has 20 multiple-choice questions. Here are the correct answers:



Your program should store these correct answers in a list. The program should read the student’s answers for each of the 20 questions from a text file and store the answers in another list. (Create your own text file to test the application.) After the student’s answers have been read from the file, the program should display a message indicating whether the student passed or failed the exam. (A student must correctly answer 15 of the 20 questions to pass the exam.) It should then display the total number of correctly answered questions, the total number of incorrectly answered questions, and a list showing the question numbers of the incorrectly answered questions.

# Store correct answers in a list
correct_answers = [
    'A', 'C', 'A', 'A', 'D',
    'B', 'C', 'A', 'C', 'B',
    'A', 'D', 'C', 'A', 'D',
    'C', 'B', 'B', 'D', 'A'
]

# Create an empty list to store student answers
student_answers = []

print("Enter your answers (A/B/C/D) for 20 questions:")

# Take input for each of the 20 questions
for i in range(20):
    ans = input(f"Question {i+1}: ").strip().upper()
    student_answers.append(ans)

# Compare and evaluate
correct_count = 0
incorrect_questions = []

for i in range(20):
    if student_answers[i] == correct_answers[i]:
        correct_count += 1
    else:
        incorrect_questions.append(i + 1)

# Print result
print("\nResult Summary:")
if correct_count >= 15:
    print("Status: PASSED ")
else:
    print("Status: FAILED ")

print("Total Correct Answers:", correct_count)
print("Total Incorrect Answers:", 20 - correct_count)
print("Incorrect Questions:", incorrect_questions)

Post a Comment

0 Comments