CSE91D Lab Evaluation 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Lab Evaluation 3

Lab Evaluation 3 Course code: CSE91D

Q1: Code:

suitable_numbers = []

for number in range(2000, 2501):


if number % 17 == 0 and number % 5 != 0:
suitable_numbers.append(number)

# Print the list of suitable numbers


print(suitable_numbers)

Q2:

Task 1: Swapping with a Temporary Variable

Code:

a = int(input("Enter the first integer (a): "))

b = int(input("Enter the second integer (b): "))


print(f"Original values: a = {a}, b = {b}")
temp = a
a=b
b = temp
print(f"Swapped values with a temporary variable: a = {a}, b = {b}")

Task 2: Swapping without a Temporary Variable


Code:
a = int(input("Enter the first integer (a): "))

b = int(input("Enter the second integer (b): "))

# Print original values

print(f"Original values: a = {a}, b = {b}")

a, b = b, a

print(f"Swapped values without a temporary variable: a = {a}, b = {b}")


Program to swap the values without using a temporary variable.
Code
# Task 1: Swapping with a Temporary Variable
# Take two integer inputs from the user
a = int(input("Enter the first integer (a): "))
b = int(input("Enter the second integer (b): "))
print(f"Original values: a = {a}, b = {b}")
temp = a
a=b
b = temp
print(f"Swapped values with a temporary variable: a = {a}, b = {b}")

# Reset the values for Task 2


a = int(input("\nEnter the first integer (a) again: "))
b = int(input("Enter the second integer (b) again: "))
print(f"Original values: a = {a}, b = {b}")
a, b = b, a
print(f"Swapped values without a temporary variable: a = {a}, b = {b}")

Q3:
Ans: A basic contact manager that allows users to add, view, and delete contacts. We'll
use the following modules:
Code:
import os

import json

import getpass

class ContactManager:

def __init__(self, filename="contacts.json"):

self.filename = filename

self.contacts = self.load_contacts()

def load_contacts(self):
if os.path.exists(self.filename):

with open(self.filename, 'r') as file:

return json.load(file)

return {}

def save_contacts(self):

with open(self.filename, 'w') as file:

json.dump(self.contacts, file, indent=4)

def add_contact(self, name, phone):

self.contacts[name] = phone

self.save_contacts()

print(f"Contact '{name}' added successfully.")

def view_contacts(self):

if self.contacts:

print("Contacts List:")

for name, phone in self.contacts.items():

print(f"Name: {name}, Phone: {phone}")

else:

print("No contacts found.")

def delete_contact(self, name):

if name in self.contacts:

del self.contacts[name]

self.save_contacts()
print(f"Contact '{name}' deleted successfully.")

else:

print(f"Contact '{name}' not found.")

def clear_screen(self):

os.system('cls' if os.name == 'nt' else 'clear')

def main():

manager = ContactManager()

while True:

manager.clear_screen()

print("Contact Manager")

print("1. Add Contact")

print("2. View Contacts")

print("3. Delete Contact")

print("4. Exit")

choice = input("Enter your choice: ")

if choice == '1':

name = input("Enter contact name: ")

phone = input("Enter contact phone number: ")

manager.add_contact(name, phone)

elif choice == '2':

manager.view_contacts()
input("\nPress Enter to continue...")

elif choice == '3':

name = input("Enter contact name to delete: ")

manager.delete_contact(name)

input("\nPress Enter to continue...")

elif choice == '4':

print("Exiting the program.")

break

else:

print("Invalid choice! Please try again.")

input("\nPress Enter to continue...")

if __name__ == "__main__":

main()

You might also like