حل lab1 مادة CS112 Object Oriented Programming جامعة الامير مقرن بن عبد العزيز
- جامعة الامير مقرن
- 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. Student Marks Analyzer: Write a program that:
a. Asks the user to enter marks of 5 students (store in a list).
b. Print all marks. c. Print the highest and lowest mark.
d. Print the average mark (rounded to 2 decimals).
# Student Marks Analyzer
marks = []
for i in range(5):
m = float(input(f"Enter mark of student {i+1}: "))
marks.append(m)
print("All marks:", marks)
print("Highest mark:", max(marks))
print("Lowest mark:", min(marks))
print("Average mark:", round(sum(marks) / len(marks), 2))
2. Word Counter: Write a program that:
a. Asks the user to enter a sentence.
b. Count how many words are in the sentence.
c. Count how many times the letter "a" appears.
d. Print the sentence in uppercase.
# Word Counter
sentence = input("Enter a sentence: ")
words = sentence.split()
count_words = len(words)
count_a = sentence.lower().count("a")
print("Number of words:", count_words)
print("Number of 'a':", count_a)
print("Uppercase sentence:", sentence.upper())
3. Simple Shopping Cart: Write a program that:
a. Has a list of available items: ["apple", "banana", "milk", "bread"].
b. Asks the user to add 3 items to a shopping cart (input one at a time).
c. Store them in a new list.
d. Print the shopping cart.
e. Print how many "apple" were bought.
# Simple Shopping Cart
items = ["apple", "banana", "milk", "bread"]
cart = []
for i in range(3):
item = input(f"Add item {i+1}: ").lower()
if item in items:
cart.append(item)
else:
print("Item not available")
print("Shopping cart:", cart)
print("Number of apples bought:", cart.count("apple"))
4. Number Statistics: Write a program that:
a. Asks the user to enter numbers until they type "stop".
b. Store the numbers in a list.
c. After stopping, print: i. All numbers entered ii. iii. iv. Sum of numbers Largest number Average of numbers
# Number Statistics
numbers = []
while True:
value = input("Enter a number (or 'stop' to finish): ")
if value.lower() == "stop":
break
numbers.append(float(value))
if numbers:
print("All numbers:", numbers)
print("Sum:", sum(numbers))
print("Largest:", max(numbers))
print("Average:", round(sum(numbers) / len(numbers), 2))
else:
print("No numbers entered.")
5. Password Checker: Write a program that:
a. Asks the user to enter a password.
b. Password is valid if: i. At least 8 characters ii. iii. Contains "@" Print "Valid" if conditions are met, otherwise "Invalid".
# Password Checker
password = input("Enter password: ")
if len(password) >= 8 and "@" in password:
print("Valid")
else:
print("Invalid")
6. Multiplication Table Generator: Write a program that:
a. Asks the user for a number. Lab Manual CS112 Object Oriented Programming
b. Prints the multiplication table of that number up to 12.
c. Each line should look like "5 x 3 = 15".
# Multiplication Table Generator
n = int(input("Enter a number: "))
for i in range(1, 13):
print(f"{n} x {i} = {n * i}")
7. Voting System: Write a program that:
a. Asks 5 students to vote for "Ali" or "Sara".
b. Store votes in a list.
c. Count votes for each candidate. d. Print the winner.
# Voting System
votes = []
for i in range(5):
vote = input(f"Vote {i+1} (Ali/Sara): ").capitalize()
if vote in ["Ali", "Sara"]:
votes.append(vote)
else:
print("Invalid vote")
ali_count = votes.count("Ali")
sara_count = votes.count("Sara")
if ali_count > sara_count:
print("Winner: Ali")
elif sara_count > ali_count:
print("Winner: Sara")
else:
print("It's a tie.")
8. Guessing Game: Write a program that:
a. Chooses a secret number between 1 and 10 (use random.randint).
b. User has 3 chances to guess it.
c. Print "Correct!" if guessed, or "Out of tries" if not.
# Guessing Game
import random
secret = random.randint(1, 10)
chances = 3
for i in range(chances):
guess = int(input("Guess the number (1-10): "))
if guess == secret:
print("Correct!")
break
else:
print("Out of tries. The number was:", secret)
9. Favorite Fruits List: Write a program that:
a. Asks the user to enter 5 favorite fruits (store them in a list).
b. Print the list.
c. Print the fruits in reverse order. d. Print only the first 3 fruits.
# Favorite Fruits List
fruits = []
for i in range(5):
fruit = input(f"Enter favorite fruit {i+1}: ")
fruits.append(fruit)
print("All fruits:", fruits)
print("Reversed order:", list(reversed(fruits)))
print("First three fruits:", fruits[:3])
10. Student Directory with Lists: Write a program that:
a. Create two lists: one for names and one for IDs.
b. Ask the user to enter 3 names and their IDs, and store them in the two lists (same index = same student).
c. Ask the user to enter a name to search.
d. If found, print that student’s ID, otherwise print "Student not found".
# Student Directory with Lists
names = []
ids = []
for i in range(3):
name = input(f"Enter student {i+1} name: ")
student_id = input(f"Enter {name}'s ID: ")
names.append(name)
ids.append(student_id)
search = input("Enter a name to search: ")
if search in names:
index = names.index(search)
print(f"{search}'s ID is {ids[index]}")
else:
print("Student not found")