You are on page 1of 4

Computer Science

Assignment-7
~By- Vishesh Aggarwal
Python Modules:
I. Program-1
# Code for value of Quadratic Formula.
# The solutions here, will depend on value of Discriminant.
def quadratic_formula(a,b,c):
import math as np
d=((b**2)-(4*a*c))
if d > 0:
print("The equation has two real solution. The value of discriminant is",d,'.')
if np.sqrt(d)%1==0:
sol1=((-b)-(np.sqrt(d)))/(2*a)
print('The first solution is ',sol1)
sol2=((-b)+(np.sqrt(d)))/(2*a)
print('The second solution is ',sol2)
else:
sol1=((str(-b)+'+'+'√'+str(d)+'/'+str(2*a)))
print('The first solution is ',sol1)
sol2=((str(-b)+'-'+'√'+str(d)+'/'+str(2*a)))
print('The second solution is ',sol2)
elif (d) < 0:
print("The equation has a complex solution.")
else:
print("The equation has only one solution.")
sol=(-b)/(2*a)
print('The only solution is',sol)
a=eval(input("Enter the coeffient of x\u00b2, i.e. a = "))
b=eval(input("Enter the coeffient of x, i.e. b = "))
c=eval(input("Enter the constant term, i.e. c = "))
quadratic_formula(a,b,c)

Output:
Enter the coeffient of x², i.e. a = 1
Enter the coeffient of x, i.e. b = -2
Enter the constant term, i.e. c = -8
The equation has two real solution. The value of discriminant is 36 .
The first solution is -2.0
The second solution is 4.0

II. Program-2
#Quiz with 10 sums.
import random
win=0
for i in range(10):
a=random.randrange(1,100)
b=random.randrange(1,100)
print("First Number :",a,"\nSecond Number :",b)
s=int(input("Can tell its sum? What is it? : "))
sum=a+b
if s==sum:
print("Great! You answered corretly.")
win+=1
else:
print("Oops! Wrong answer")
print("Thank you for playing. The Quiz is over. \n\tResults:")
print("You answered",win,"questions correctly (out of 10.)")
print("No. of Wrong Answers :",10-win)

Output:
First Number : 82
Second Number : 13
Can tell its sum? What is it? : 95
Great! You answered corretly.
First Number : 22
Second Number : 38
Can tell its sum? What is it? : 60
Great! You answered corretly.
First Number : 48
Second Number : 38
Can tell its sum? What is it? : 86
Great! You answered corretly.
First Number : 17
Second Number : 4
Can tell its sum? What is it? : 21
Great! You answered corretly.
First Number : 12
Second Number : 5
Can tell its sum? What is it? : 17
Great! You answered corretly.
First Number : 33
Second Number : 8
Can tell its sum? What is it? : 41
Great! You answered corretly.
First Number : 18
Second Number : 60
Can tell its sum? What is it? : 78
Great! You answered corretly.
First Number : 85
Second Number : 39
Can tell its sum? What is it? : 124
Great! You answered corretly.
First Number : 89
Second Number : 69
Can tell its sum? What is it? : 158
Great! You answered corretly.
First Number : 34
Second Number : 65
Can tell its sum? What is it? : 99
Great! You answered corretly.
Thank you for playing. The Quiz is over.
Results:
You answered 10 questions correctly (out of 10.)
No. of Wrong Answers : 0
III. Program-3
#Mean & Median of given Data
list1=list(eval(input("Enter all the numbers (sepreated by commas): ")))
import statistics
mean=statistics.mean(list1)
median=statistics.median(list1)
print("The Mean & Median of these numbers is :",mean,"&",median)

Output:
Enter all the numbers (sepreated by commas): 23,45,78,45,67,34,52,79
The Mean & Median of these numbers is : 52.875 & 48.5

Merry Christmas
Thank You

You might also like