0% found this document useful (0 votes)
3 views4 pages

Python Modules

The document contains Python programs for three tasks: calculating the roots of a quadratic equation based on user input, generating two random numbers for the user to guess their sum, and creating a list of numbers to compute their mean and median. Each program includes user prompts and outputs results based on the computations. The examples demonstrate how to handle real and imaginary roots, user guesses, and statistical calculations.

Uploaded by

aayushchetiwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Python Modules

The document contains Python programs for three tasks: calculating the roots of a quadratic equation based on user input, generating two random numbers for the user to guess their sum, and creating a list of numbers to compute their mean and median. Each program includes user prompts and outputs results based on the computations. The examples demonstrate how to handle real and imaginary roots, user guesses, and statistical calculations.

Uploaded by

aayushchetiwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 4

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 + math.sqrt(D)) / (2 * a)

x2 = (-b - math.sqrt(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=rd.randint(0, 100)

x2=rd.randint(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: '))


l.append(e)

print('Mean: ', st.mean(l))

print('Median: ', st.median(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

You might also like