Skip to content

Commit 19c3d3c

Browse files
committed
Assignment 2 codes pushed
1 parent b8de1da commit 19c3d3c

11 files changed

Lines changed: 293 additions & 0 deletions

Assignment 2/arith_op.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
Program: WAP that reads two numbers and an arithmetic operator and displays the result.
3+
Author: Bikramadittya Bagchi
4+
Date: 28-01-2021
5+
'''
6+
7+
inp1 = float(input("Enter first number: "))
8+
inp2 = float(input("Enter second number: "))
9+
op = input("Enter the operation [+,-,*,/,%]: ")
10+
11+
if op in ['+', '-', '*', '/', '%']:
12+
if op == '+':
13+
result = inp1 + inp2
14+
print(f"Result of {op} is {result}")
15+
elif op == '-':
16+
result = inp1 - inp2
17+
print(f"Result of {op} is {result}")
18+
elif op == '*':
19+
result = inp1 * inp2
20+
print(f"Result of {op} is {result}")
21+
elif op == '/':
22+
if inp2 == 0:
23+
print("Division by 0 not allowed! Exiting...")
24+
else:
25+
result = inp1 / inp2
26+
print(f"Result of {op} is {result}")
27+
elif op == '%':
28+
result = inp1 % inp2
29+
print(f"Result of {op} is {result}")
30+
else:
31+
print("Operator not defined! Please define operators as +,-,*,/,%")

Assignment 2/avg_grading.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'''
2+
Program: WAP to determine a student’s final grade and indicate whether
3+
they are passing or failing. The final grade is calculated as the average of marks of four subjects.
4+
Author: Bikramadittya Bagchi
5+
Date: 28-01-2021
6+
'''
7+
8+
sub1 = float(input("Enter the marks of Subject 1: "))
9+
sub2 = float(input("Enter the marks of Subject 2: "))
10+
sub3 = float(input("Enter the marks of Subject 3: "))
11+
sub4 = float(input("Enter the marks of Subject 4: "))
12+
13+
avg = (sub1+sub2+sub3+sub4)/4
14+
if avg >= 40:
15+
print(f"Your Grade is: {avg}")
16+
print("Congratulations! You have passed!")
17+
else:
18+
print(f"Your grade is: {avg}")
19+
print("Sorry! You failed!")

Assignment 2/div5or3.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'''
2+
Program: WAP to input a number and check whether the number is divisible by 5 and 3 or not.
3+
Author: Bikramadittya Bagchi
4+
Date: 01-02-2021
5+
'''
6+
import re
7+
8+
inp = input("Enter a number: ")
9+
regex_float = '[+-]?[0-9]+\.[0-9]+'
10+
regex_int = "[-+]?[0-9]+$"
11+
if re.search(regex_float, inp) or re.search(regex_int, inp):
12+
inp = float(inp)
13+
print("Checking whether the number is divisible by 5 and 3 or not.", end=' ')
14+
if inp % 5 == 0 and inp % 3 == 0:
15+
print(f"The number {inp} is divisible by 5 and 3")
16+
else:
17+
print(f"The number {inp} is NOT divisible by 5 and 3")
18+
else:
19+
print("Entered input is not a number!")

Assignment 2/electricity_calc.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'''
2+
Program: WAP to input consumed unit and calculate Electricity Bill, where monthly rent Rs. 300.
3+
Unit Charge per unit(Rs.)
4+
Upto 300 7
5+
301- 800 9
6+
801-1500 12
7+
1501 & above 15
8+
Author: Bikramadittya Bagchi
9+
Date: 02-02-2021
10+
'''
11+
# Declarations
12+
total = 0.0
13+
print("***Electricity bill calculator***")
14+
15+
# Taking input from users
16+
while True:
17+
try:
18+
inp = input("Please enter the units consumed: ")
19+
inp = int(inp)
20+
break
21+
except ValueError:
22+
print("Not a valid input! Enter units in numbers only!")
23+
if inp >= 0:
24+
min_rent = 300
25+
# Processing data
26+
if inp <= 300:
27+
if (inp * 7) < min_rent:
28+
total = min_rent
29+
else:
30+
total = inp * 7
31+
elif 300 < inp <= 800:
32+
total = ((inp - 300) * 9) + (300 * 7)
33+
elif 800 < inp <= 1500:
34+
total = (300 * 7) + (500 * 9) + ((inp - 800) * 12)
35+
else:
36+
total = (300 * 7) + (500 * 9) + (700 * 12) + ((inp - 1500) * 15)
37+
# Display output
38+
print(f"Consumed unit: {inp}")
39+
print(f"Your total bill {total}")
40+
else:
41+
print("Invalid unit entered!")

Assignment 2/grading.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'''
2+
Program: WAP to input marks of 5 subject and find average and assign grade.
3+
Average Marks Grade
4+
90 & above O
5+
80 – 89 E
6+
70-79 A
7+
Below 70 B
8+
Author: Bikramadittya Bagchi
9+
Date: 02-02-2021
10+
'''
11+
# Declarations
12+
marks_list = []
13+
total = 0.0; avg = 0.0
14+
15+
# Taking input from user
16+
while True:
17+
for i in range(5):
18+
try:
19+
usr_inp = input("Please enter a marks: ")
20+
usr_inp = float(usr_inp)
21+
if usr_inp >= 0 and usr_inp <= 100:
22+
marks_list.append(usr_inp)
23+
else:
24+
print("Entered marks not in range of 0 and 100! Skipping this entry")
25+
except ValueError:
26+
print("Invalid data entered! Please enter a valid marks")
27+
break
28+
else:
29+
break
30+
31+
if len(marks_list) == 5:
32+
for i in range(5):
33+
total += marks_list[i]
34+
avg = total/len(marks_list)
35+
print(f"Average marks is {avg}")
36+
if avg >= 90:
37+
print("Grade is O")
38+
elif avg >= 80:
39+
print("Grade is E")
40+
elif avg >= 70:
41+
print("Grade is A")
42+
else:
43+
print("Grade is B")
44+
else:
45+
print("Incomplete data provided!")

Assignment 2/largest_2nos.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'''
2+
Program: WAP to find the largest number between two inputted numbers
3+
Author: Bikramadittya Bagchi
4+
Date: 28-01-2021
5+
'''
6+
7+
num1 = float(input('Enter number 1: '))
8+
num2 = float(input('Enter number 2: '))
9+
10+
if num1 > num2:
11+
print("First number is largest!")
12+
else:
13+
print("Second number is largest")

Assignment 2/second_smallest.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'''
2+
Program: WAP to input 3 numbers and find the second smallest.
3+
Author: Bikramadittya Bagchi
4+
Date: 02-02-2021
5+
'''
6+
import re
7+
8+
# Declarations
9+
regex_float = '[+-]?[0-9]+\.[0-9]+'
10+
regex_int = "[-+]?[0-9]+$"
11+
small_list = []
12+
small_1 = 0
13+
small_2 = 0
14+
# Taking input from users
15+
print("Enter 3 numbers for finding smallest among them")
16+
for i in range(3):
17+
inp = input("Enter any number to insert: ")
18+
if re.search(regex_float, inp):
19+
inp = float(inp)
20+
small_list.append(inp)
21+
elif re.search(regex_int, inp):
22+
inp = int(inp)
23+
small_list.append(inp)
24+
else:
25+
print("Invalid data entered!")
26+
break
27+
28+
# Processing data
29+
if len(small_list) == 3:
30+
for item in small_list[1:]:
31+
if item < small_1:
32+
small_2 = small_1
33+
small_1 = item
34+
elif small_2 == 0 or small_2 > item:
35+
small_2 = item
36+
# Display output
37+
print("The entered elements are: ", end=" ")
38+
for item in small_list:
39+
print(item, end=" ")
40+
print(f"and, the second smallest number is: {small_2}")
41+
else:
42+
print("Unable to compute data! Insufficient data received!")

Assignment 2/triangle_detect.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'''
2+
Program: WAP to input 3 side of a triangle and prints its type (i.e Equilateral/Isosceles/Scalene).
3+
Author: Bikramadittya Bagchi
4+
Date: 02-02-2021
5+
'''
6+
# Declarations
7+
sides_list = []
8+
i = 3
9+
# Take input from user
10+
while True:
11+
while i > 0:
12+
try:
13+
usr_inp = input("Enter the side of a triangle: ")
14+
usr_inp = float(usr_inp)
15+
i = i - 1
16+
if usr_inp >= 0 and usr_inp <= 60:
17+
sides_list.append(usr_inp)
18+
else:
19+
print("Entered input is out of range [0 - 60]. Discarded the input")
20+
except ValueError:
21+
print("Entered input is not valid! Please re-enter")
22+
break
23+
else:
24+
break
25+
26+
# Processing data
27+
if len(sides_list) == 3:
28+
if sides_list[0] == sides_list[1] == sides_list[2]:
29+
print("The triangle is Equilaterial")
30+
elif sides_list[0] == sides_list[1] or sides_list[1] == sides_list[2] or sides_list[2] == sides_list[0]:
31+
print("The triangle is Isosceles")
32+
else:
33+
print("The triangle is Scalene")
34+
else:
35+
print(f"Sides of triangle received is {len(sides_list)}. Hence, unable to display appropriate result!")

Assignment 2/two_digit_check.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'''
2+
Program: WAP to input a number and check whether the number is 2 digit number or not.
3+
Author: Bikramadittya Bagchi
4+
Date: 01-02-2021
5+
'''
6+
import re
7+
8+
inp = input("Enter a number: ")
9+
regex_float = '[+-]?[0-9]+ .[0-9]+'
10+
regex_int = "[-+]?[0-9]+$"
11+
if re.search(regex_float, inp) or re.search(regex_int, inp):
12+
f_no = float(inp)
13+
s_no = int(f_no)
14+
c = 0
15+
diff = (f_no - s_no)
16+
if diff == 0:
17+
while f_no > 0:
18+
r = f_no % 10
19+
f_no = f_no // 10
20+
c+=1
21+
if c == 2:
22+
print(f"Entered number {inp} is a 2 Digit number")
23+
else:
24+
print(f"Entered number {inp} is not a 2 Digit number")
25+
else:
26+
print("Entered input is not a number")

Assignment 2/vowel_conso.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'''
2+
Program: WAP to accept a character from the user and display whether it is a vowel or consonant.
3+
Author: Bikramadittya Bagchi
4+
Date: 28-01-2021
5+
'''
6+
7+
character = input("Enter any character to check vowel or consonant: ")
8+
if character[0] in ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u']:
9+
print(f"Entered character {character[0]} is a VOWEL!")
10+
else:
11+
print(f"Entered character {character[0]} is a CONSONANT!")

0 commit comments

Comments
 (0)