حل لاب3 برمجة غرضية التوجه جامعة الامير مقرن بن عبد العزيز
- جامعة الامير مقرن
- CS112 Object Oriented Programming - CS112 برمجة غرضية التوجه
- برمجة بايثون | Python programming
- 2025-10-15
توصيف
Please do the following exercises and submit python files during the lab time.
Create a different file for every code.
1. Fruit Basket:
A shopkeeper wants to record the weight of 4 fruit baskets.
a. Ask the user to enter 4 weights (kg).
b. Print the heaviest and lightest basket.
# Fruit Basket Weights
weights = []
for i in range(4):
w = float(input(f"Enter weight of basket {i+1} (kg): "))
weights.append(w)
print("Heaviest basket:", max(weights), "kg")
print("Lightest basket:", min(weights), "kg")
2. Movie Tickets: A cinema sells tickets for 25 SAR each.
a. Ask the user how many adults and how many children came.
b. Children pay half price.
c. Print the total cost.
# Movie Tickets Calculator
adult_tickets = int(input("Enter number of adults: "))
child_tickets = int(input("Enter number of children: "))
adult_price = 25
child_price = adult_price / 2
total = (adult_tickets * adult_price) + (child_tickets * child_price)
print("Total cost:", total, "SAR")
3. Exam Grades: Ask the user to enter 6 exam marks.
a. Store them in a list.
b. Print how many marks are passing (≥ 60) and how many are failing.
marks=[]
passing_counter=0
failing_counter=0
for mark in range(6):
mark=int(input("enter the mark:"))
marks.append(mark)
if mark>=60:
passing_counter+=1
else: failing_counter+=1
print("number of passing marks: ",passing_counter)
print("number of failing marks: ",failing_counter)
4. Password Strength Checker:
Ask the user to enter a password.
a. If the password length < 6 → “Weak”
b. If it contains numbers and letters → “Strong”
c. Otherwise → “Medium”
password=input("enter the password:")
has_digit=False
has_letter=False
if len(password)<6:
print("weak")
else:
for p in password:
if p.isdigit():
has_digit=True
if p.isalpha():
has_letter=True
if has_digit and has_letter:
print("strong")
else: print("Medium")
5. Cafeteria Menu: A cafeteria has a menu: ["tea", "coffee", "sandwich", "juice"].
a. Ask the user to order 3 items (input each one).
b. If an item is not in the menu, print “Not available”.
c. At the end, print the final order list.
menu = ["tea", "coffee", "sandwich", "juice"]
order = []
for i in range(3):
item = input(f"Enter item {i+1}: ").lower()
if item in menu:
order.append(item)
else:
print("Not available")
print("Final order:", order)
6. Multiples Game: Ask the user for a number n.
a. Print all multiples of n from 1 to 100.
b. At the end, print how many multiples were found.
# Multiples Game
n = int(input("Enter a number: "))
multiples = []
for i in range(1, 101):
if i % n == 0:
multiples.append(i)
print("Multiples of", n, "from 1 to 100:", multiples)
print("Total multiples found:", len(multiples))
7. Parking Lot Fees: A parking lot charges 5 SAR per hour.
a. Ask the user how many hours 3 cars stayed.
b. Store the hours in a list.
c. For each car, print the fee (hours × 5).
d. At the end, print the total fee for all cars.
# Parking Lot Fees
hours = []
for i in range(3):
h = float(input(f"Enter hours for car {i+1}: "))
hours.append(h)
total_fee = 0
for i, h in enumerate(hours, start=1):
fee = h * 5
print(f"Car {i} fee: {fee} SAR")
total_fee += fee
print("Total fee for all cars:", total_fee, "SAR")
8. Temperature Records: Ask the user to enter the temperature for 7 days.
a. Store in a list.
b. Print the average temperature.
c. Print how many days were above average.
# Temperature Records
temps = []
for i in range(7):
t = float(input(f"Enter temperature for day {i+1}: "))
temps.append(t)
avg_temp = sum(temps) / len(temps)
above_avg = sum(1 for t in temps if t > avg_temp)
print("Average temperature:", round(avg_temp, 2))
print("Days above average:", above_avg)
9. Student Class (with User Input):
Create a class `Student` with attributes `name` and `student_id`.
a. Constructor should initialize both.
b. Add getters, setters, and `__str__`.
c. Ask the user to enter data for 2 students, create objects, and print them.
# Student Class
class Student:
def __init__(self, name, student_id):
self.name = name
self.student_id = student_id
def get_name(self):
return self.name
def get_id(self):
return self.student_id
def set_name(self, name):
self.name = name
def set_id(self, student_id):
self.student_id = student_id
def __str__(self):
return f"Student Name: {self.name}, ID: {self.student_id}"
students = []
for i in range(2):
name = input(f"Enter name for student {i+1}: ")
sid = input(f"Enter ID for {name}: ")
s = Student(name, sid)
students.append(s)
for s in students:
print(s)
10. Book Class (with User Input):
Create a class `Book` with attributes `title`, `author`, and `pages`.
a. Constructor should initialize all attributes.
b. Add getters, setters, and `__str__`.
c. Ask the user to enter details for 2 books.
d. Print both books, and check if any book has more than 200 pages.
# Book Class
class Book:
def __init__(self, title, author, pages):
self.title = title
self.author = author
self.pages = pages
def get_title(self):
return self.title
def get_author(self):
return self.author
def get_pages(self):
return self.pages
def set_title(self, title):
self.title = title
def set_author(self, author):
self.author = author
def set_pages(self, pages):
self.pages = pages
def __str__(self):
return f"Book Title: {self.title}, Author: {self.author}, Pages: {self.pages}"
books = []
for i in range(2):
title = input(f"Enter title for book {i+1}: ")
author = input(f"Enter author for {title}: ")
pages = int(input(f"Enter number of pages for {title}: "))
b = Book(title, author, pages)
books.append(b)
for b in books:
print(b)
if b.get_pages() > 200:
print("This book has more than 200 pages.")