0% found this document useful (0 votes)
10 views10 pages

Python Tw2

Uploaded by

yATHARTH Tyagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

Python Tw2

Uploaded by

yATHARTH Tyagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Gaurav Satyawali MCA AI & DS) Sec-A

25210820077/ R.No.27

PROBLEM STATEMENT 8 : In a university portal, students should not register for the same
course twice. Use sets to maintain

registered courses. Write a Python program that:

 Takes course codes as input until the student types "done".

 Stores them in a set.

 Displays the final list of registered courses in order it will add.

 If a student tries to register for the same course again, display: "Already registered!”

Test Case:

Enter course code (or 'done' to finish): TMD 101

Course added successfully!

Enter course code (or 'done' to finish): TMD 102

Course added successfully!

Enter course code (or 'done' to finish): TMD 103

Course added successfully!

Enter course code (or 'done' to finish): TMD 104

Course added successfully!

Enter course code (or 'done' to finish): TMD 102

Already registered!

Enter course code (or 'done' to finish): TMD 105

Course added successfully!

Enter course code (or 'done' to finish): TMD 106

Course added successfully!

Enter course code (or 'done' to finish): TMD 104

Already registered!

Enter course code (or 'done' to finish): done

Final Registered Courses are: ['TMD 101', 'TMD 102', 'TMD 103', 'TMD 104', 'TMD 105',
'TMD 106']

15
Gaurav Satyawali MCA AI & DS) Sec-A
25210820077/ R.No.27

CODE :

registered_courses = set()

course_list = []

while True:

course = input("Enter course code (or 'done' to finish): ")

if course.lower() == "done":

break

if course in registered_courses:

print("Already registered!")

else:

registered_courses.add(course)

course_list.append(course)

print("Course added successfully!")

print("\nFinal Registered Courses are:", course_list)

OUTPUT :

16
Gaurav Satyawali MCA AI & DS) Sec-A
25210820077/ R.No.27

PROBLEM STATEMENT 9 : An IT company wants to shortlist students who


know both Python and NoSQL. Write a python

program:

 Take student names for Python and NoSQL knowledge one by


one.

 Stop input when user enters "DONE".

 Find the intersection (students in both).

 Print the shortlisted names or "No students match the criteria".

Test Case 1:

Enter names of Python students (type DONE to stop):

Ria

Gianna

Rohit

BOB

Saket

Anuj

done

Enter names of NoSQL students (type DONE to stop):

gianna

17
Gaurav Satyawali MCA AI & DS) Sec-A
25210820077/ R.No.27

manisha

sAKET

BOB

ANISH

RAJ

DONE

Shortlisted Students: {'SAKET', 'BOB', 'GIANNA'}

Test Case 2:

Enter names of Python students (type DONE to stop):

Riya

Mohit

DONE

Enter names of NoSQL students (type DONE to stop):

Rahul

Neha

DONE

CODE :

print("Enter names of Python students (type DONE to stop):")

python_students = set()

while True:

name = input().strip()

if name.lower() == "done":

break

python_students.add(name.upper())

print("Enter names of NoSQL students (type DONE to stop):")

nosql_students = set()

18
Gaurav Satyawali MCA AI & DS) Sec-A
25210820077/ R.No.27

while True:

name = input().strip()

if name.lower() == "done":

break

nosql_students.add(name.upper())

shortlisted = python_students.intersection(nosql_students)

if shortlisted:

print("Shortlisted Students:", shortlisted)

else:

print("No students match the criteria")

OUTPUT :

19
Gaurav Satyawali MCA AI & DS) Sec-A
25210820077/ R.No.27

PROBLEM STATEMENT 10 : A university library maintains records of books. Write a python


program to:

1. Allow users to borrow books (store in a set).

2. Prevent duplicate borrowing (check using conditional statement).

3. Allow users to return books.

4. Display final borrowed books.

Test Case 1:

University Library System

Choose: BORROW, RETURN, EXIT:

Enter action (BORROW/RETURN/EXIT): return

Enter book name: software engineering

Book not found in borrowed list.

CODE :

borrowed_books = set()

print("University Library System")

20
Gaurav Satyawali MCA AI & DS) Sec-A
25210820077/ R.No.27

print("Choose: BORROW, RETURN, EXIT:")

while True:

action = input("Enter action (BORROW/RETURN/EXIT): ")

if action.lower() == "borrow":

book = input("Enter book name: ").upper()

if book in borrowed_books:

print(book, "already borrowed!")

else:

borrowed_books.add(book)

print(book, "borrowed successfully.")

elif action.lower() == "return":

book = input("Enter book name: ").upper()

if book in borrowed_books:

borrowed_books.remove(book)

print(book, "returned successfully.")

else:

print("Book not found in borrowed list.")

elif action.lower() == "exit":

break

else:

print("Invalid action! Please enter BORROW, RETURN, or EXIT.")

print("Final Borrowed Books:", borrowed_books)

OUTPUT :

21
Gaurav Satyawali MCA AI & DS) Sec-A
25210820077/ R.No.27

PROBLEM STATEMENT 11 : Design a program to analyze student results. Each


student is represented as a tuple:
(roll_number, name, [marks in subjects])
You are given a list of such student records. You need to:

1. Calculate the average marks for each student.

2. Assign grades based on average:

• A: 85+
• B: 70–84
• C: 50–69
• F: below 50

3. Return a list of tuples


with: (roll_number, name,
average, grade)

Test Case:

Input:

students = [
(101, "Aman", [78, 89, 91]),

22
Gaurav Satyawali MCA AI & DS) Sec-A
25210820077/ R.No.27

(102, "Baby", [56, 62, 70]),


(103, "Suzan", [45, 39, 50]),
(104, "Diva", [88, 92, 85])
]

Output:

(101, 'Aman', 86.0, 'A'),


(102, 'Baby', 62.67, 'C'),
(103, 'Suzan', 44.67, 'F'),
(104, 'Diva', 88.33, 'A')
]

CODE :

students = [

(101, "Aman", [78, 89, 91]),

(102, "Baby", [56, 62, 70]),

(103, "Suzan", [45, 39, 50]),

(104, "Diva", [88, 92, 85])

results = []

for roll, name, marks in students:

avg = sum(marks) / len(marks)

if avg >= 85:

grade = "A"

elif avg >= 70:

grade = "B"

elif avg >= 50:

grade = "C"

23
Gaurav Satyawali MCA AI & DS) Sec-A
25210820077/ R.No.27

else:

grade = "F"

results.append((roll, name, round(avg, 2), grade))

print(results)

OUTPUT :

24

You might also like