Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
18 views3 pages

Home Assignment 2

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

Application Development

Home Assignment #2
(100 Marks= 5 Grade Points), Deadline: October 02, 2024
Instructions:
• Write the python code for the following in Jupyter Notebook.
• Use proper markdown cells to create headings and formatted text.
• Run each program with sample input values to show the output.
T

This assignment covers conditional statements (if, elif, else) and loops (for, while) , Functions , Files
and Exception Handling in Python.

I. Conditional Statements (20 points)


1. Write a program that asks the user for a number. If the number is positive, print "The number is
positive". If it's negative, print "The number is negative". Otherwise, print "The number is zero".
2. Write a program that asks the user for a grade (between 0 and 100). Use an if-elif-else chain to
print the corresponding letter grade: A (90-100), B (80-89), C (70-79), D (60-69), F (below 60).
3. Write a program that checks if two numbers are equal. If they are, print "The numbers are
equal". Otherwise, print "The numbers are different".
4. Write a program that asks the user for a day of the week (as a string). Use an if-elif-else chain
to print a message for each day (e.g., "It's Monday!" for Monday).
5. Write a program that checks if a string is empty. If it is, print "The string is empty". Otherwise,
print "The string is not empty".
6. (Bonus) Write a program that simulates a simple password checker. Ask the user for a
password and check if it meets certain criteria (e.g., minimum length, contains uppercase and
lowercase letters). Use conditional statements to print appropriate messages.
7. Write a program that plays a guessing game. Choose a random number between 1 and 100.
The user must guess the number in a limited number of attempts. Use a loop and conditional
statements to handle the guesses and provide feedback.
8. Write a program that converts a temperature from Celsius to Fahrenheit or vice versa. Use an
if statement to ask the user for the conversion direction (C to F or F to C) and then convert the
temperature using a formula.
9. Write a program that checks if a given year is a leap year. Use conditional statements to check
the divisibility rules for leap years.
10. Write a program that simulates a simple ATM machine. The user has a starting balance and
can perform transactions (deposit, withdrawal). Use conditional statements to check if the
balance is sufficient for withdrawals and print appropriate messages.

II. Loops (20 points)

1. Write a program that prints all even numbers from 1 to 20 (inclusive) using a for loop.
2. Write a program that calculates the factorial of a number entered by the user. Use a for loop to
multiply the number by all positive integers less than itself.
3. Write a program that prints the first 10 elements of the Fibonacci sequence (1, 1, 2, 3, 5, ...).
Use a for loop to calculate each element based on the previous two.
4. Write a program that prints a square pattern of stars (*) using a nested loop. The user enters
the size of the square.
5. Write a program that reads a list of numbers (until the user enters 0) and calculates the sum of
all positive numbers. Use a while loop to keep reading numbers until the sentinel value (0) is
entered.
6. Write a program that prints a multiplication table for a given number entered by the user. Use
nested loops to iterate through the rows and columns.
7. Write a program that plays a simple guessing game similar to question 7 in the conditional
statements section, but this time use a while loop to keep asking for guesses until the user
gets it right.
8. Write a program that checks if a given string is a palindrome (reads the same backward as
forward). Use a loop to iterate through the string and compare characters from both ends.
9. Write a program that simulates a simple quiz with multiple choice questions. Store the
questions and answers in lists. Use a loop to iterate through the questions, display them, and
check the user's answers.
10. Write a program that encrypts or decrypts a message using a Caesar cipher. Shift each letter
in the message by a certain number of positions (positive for encryption, negative for
decryption). Use a loop to iterate through the characters and perform the shift.

III. Functions (40 Points)

1. Write a program that implements a calculator using functions. (5 X2=10)

i. You should include three modes namely Arithmetic, Sets and Binary Arithmetic.
a. Arithmetic operations should include addition, subtraction, multiplication, and division
using a separate function for each operation.
b. Set operations should include union, intersection difference and symmetric difference.
Use a separate function for each.
c. Binary Arithmetic should include addition, subtraction, multiplication and division. Use a
separate function for each.
ii. You should use a main function to ask the user to choose the mode and then to input two
operands and then ask his/her choice about the desired operation.
a. Based on choice, call the relevant function which should take two parameters and then
return the result.
iii. Display the results in the main function. It is better to use a separate function for input and
output if possible.
iv. Call the function for each mode and all possible operations.

Your notebook should include proper headings mentioning all the parts clearly.

2. Write a python program that: (10)


i. Ask the user to input any seven letters from the keyboard as the letters available on the
scrabble rack.
ii. It should compute all the possible words that could be made from these letters.
iii. Then only display the valid dictionary words out of all possible combinations.
Write a separate function for each of the above i, ii and iii and control the flow from the main function.

3. Write python functions for the following: (10 x2=20)


i. Finding the LCM of two numbers
ii. Finding the LCM of any number of arbitrary arguments
iii. Finding the HCF of two number
iv. Finding the HCF of any number of arbitrary arguments
v. Display the elements of Fibonacci series.
vi. Binary search using functions.
vii. Binary search using recursion.
viii. Merge Sort using functions.
ix. Merge sort using recursion.
x. Convert a decimal number into binary number.
IV. Files and Exception Handling ( 20 Points)
1. Write a program takes sales data from the user, saves it in a file and then retrieves the
data back from the file. Do the following: (10)

a) Ask the user how many sales records to enter (1)


b) Take the data from the user and write it into a file. (3)
c) Close and open the file for reading purposes. Compute total sales, average sales, minimum
and maximum sales from the file. (3)
d) Apply necessary exception handling where required. (3)
Use three functions; a main function to control the flow, a function that takes data from the user and
saves it into a file, and a function that retrieves data from the file and returns total, average, min and
max sales.

2. Write a program that takes a text file as an input and does the following: (10)

i. Computes number of words in the file


ii. Computes number of sentences in the file
iii. Computes the number of unique words in the file.
iv. Computes number of prepositions used.
v. Computes number of three letter words in the file

Make a separate function for each of the above and control the flow from the main (). You should call
the function and display the results in a well-formatted manner.

Submission Guidelines:

1. Submit all the above in the form of a python notebook as well as pdf. Your notebook / pdf
should be a compiled one showing all the necessary outputs.
2. Your python notebook should have proper headings clearly mentioning the question
numbers and subsequent parts etc.
3. Submit the printout of the pdf as well if required.

You might also like