Python Modules
Aayush
#A program to input three coefficients of a quadratic equation and display the
roots if they are real, otherwise the message that the roots are imaginary.
import math
a = eval(input("Enter the coefficient of x^2: "))
b = eval(input("Enter the coefficient of x: "))
c = eval(input("Enter the constant term: "))
D = (b**2) - 4 * (a * c)
if D>= 0:
x1 = (-b + [Link](D)) / (2 * a)
x2 = (-b - [Link](D)) / (2 * a)
print("The roots are:", x1, "and", x2)
else:
print("The roots are imaginary.")
Output: Enter the coefficient of x^2: 1
Enter the coefficient of x: -1
Enter the constant term: -1
The roots are: 1.618033988749895 and -0.6180339887498949
#A script that generates 2 random numbers between 0-100 and asks the user to
predict their sum. The program displays the total number of correct and wrong
guesses.
import random as rd
right=0
wrong=0
for i in range(10):
x1=[Link](0, 100)
x2=[Link](0, 100)
sum=x1+x2
guess=int(input('What is your guessed sum? '))
if guess==sum:
print('Correct!')
right+=1
else:
print('Wrong! Try again.')
wrong+=1
print('You got', right, 'right and', wrong, 'wrong.')
Output: What is your guessed sum? 2
Wrong! Try again.
What is your guessed sum? 25
Wrong! Try again.
What is your guessed sum? 67
Wrong! Try again.
What is your guessed sum? 16
Wrong! Try again.
What is your guessed sum? 45
Wrong! Try again.
What is your guessed sum? 33
Wrong! Try again.
What is your guessed sum? 43
Wrong! Try again.
What is your guessed sum? 27
Wrong! Try again.
What is your guessed sum? 19
Wrong! Try again.
What is your guessed sum? 73
Wrong! Try again.
You got 0 right and 10 wrong.
#To create a list of n numbers input by the user and display the mean and the
median.
import statistics as st
n=int(input('Enter the length of the list: '))
l=[]
for i in range(n):
e=eval(input('Enter an element: '))
[Link](e)
print('Mean: ', [Link](l))
print('Median: ', [Link](l))
Output: Enter the length of the list: 5
Enter an element: 2.1
Enter an element: -6.4
Enter an element: 9.2
Enter an element: 3.7
Enter an element: -2.9
Mean: 1.14
Median: 2.1