0% found this document useful (0 votes)
4 views7 pages

Python Test Questions 2(Intermediate)

Uploaded by

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

Python Test Questions 2(Intermediate)

Uploaded by

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

Write a Program which accepts the radius of a circle

from the user and compute the area.

Ans:-
pi =
3.14
while True:
try:
rad = input("Enter radius : ")
rad = float(rad)
area = pi * rad**2
print('The area of the circle with
radius ',rad , 'is ', area)
break
except:
print('Enter a valid radius.')

Write a Program to check if a number is positive,


negative or zero.

whil
e
True
:
try:
num = input('Enter a number to check
its nature : ')
num = float(num)
if num > 1:
print('positive')
elif num < 1:
print('negetive')
elif num == 0:
print('zero')
break
except:
print('Invalid entry.')

Write a Program to check whether a number is completely


divisible by another number

whil
e
True
:
try:
num1 = input('Enter divisor : ')
num2 = input('Enter divident : ')
num1 = int(num1)
num2 = int(num2)
if num1 % num2 == 0:
print('The two numbers are
completely divisble by eachother.')
else:
print('The two numbers are not
completely divisble by eachother.')
break
except:
print('Invalid entry. Enter integer
values.')

Write a Program to calculate number of days between two


dates

whil
e
True
:
try:
print('Enter date in the format :
dd/mm/yy')
date1 = input('Enter the first date :
')
date2 = input('Enter the second date :
')
date_1 = date1.split('/')
date_2 = date2.split('/')
diff = (int(date_2[2]) * 365 +
int(date_2[1]) * 30 + int(date_2[0])) -
(int(date_1[2]) * 365 + int(date_1[1]) * 30 +
int(date_1[0]))
print('The number of days between' ,
date2 , 'and' , date1 , 'is' , diff ,'days.')
break
except:
print('Follow the format and use digits
only')

Write a Program to get the volume of a sphere V=4 / 3


πr3

def
main()
:
while True:
try:
rad = input('Enter the radius of
the sphere : ')
rad = float(rad)
volume(rad)
break
except:
print('Invalid entry. Enter
digits only.')

def volume(r):
pi = 3.14
vol = (4 / 3) * pi * (r ** 3)
print('The volume of the sphere is' ,
vol)

main()
Write a Program to get the difference between a given
number and 17, difference cannot be negative

def
main()
:
while True:
try:
num = input('Enter number to get
the difference between it and 17 : ')
diff(num)
break
except:
print('Invalid entry. Enter
digits')

def diff(n):
n = float(n)
if n > 17:
print('Enter a number lesser than 17,
as the difference cannot be negetive.')
main()
else:
difference = 17 - n
print('The difference between' , n ,
' and 17 is' , difference)

main()

Write a program to test whether a passed letter is a


vowel or not

def
main()
:
letter = input('Enter a letter to
check if its a vowel or not : ')
check(letter)
def check(let):
if let == 'a' or let == 'e' or let == 'i'
or let == 'o' or let == 'u' or let =='A' or
let == 'E' or let == 'I' or let == 'O' or let
== 'U':
print('The letter is a vowel')
else:
print('The letter is not a vowel')
main()

Write a program to find area of a triangle

def
main()
:
while True:
try:
print('To calculate the area of a
triangle')
base = input('Enter its base: ')
height = input('Enter its
height : ')
base = float(base)
height = float(height)
area(base, height)
break
except:
print('Invalid entry, digits must
be entered.')
def area(b,h):
area = 1 / 2 * ( b * h )
print('The area of the triangle is' ,
area)
main()
Write a program that will return true if the two given
integer values are equal or their sum or difference is
5

def
main()
:
while True:
try:
num1 = input('Enter first integer
: ')
num2 = input('Enter second
integer : ')
num1 = int(num1)
num2 = int(num2)
value = check(num1, num2)
print(value)
break
except:
print('Invalid entry, enter
integral values.')
def check(n1, n2):
if n1 == n2 or n1 - n2 == 5 or n1 + n2 ==
5:
return True
else:
return False
main()

Write a program to convert height (in feet and inches)


to centimetres

def
main()
:
while True:
try:
print('Enter the height in the
following format ft,inches')
height = input('Enter height : ')
h = height.split(',')
centi = float(h[0]) * 30.48 +
float(h[1]) * 2.54
print('The given height in
centimeters is' , centi , 'cm')
break
except:
print('Invalid entry, follow the
format')
main()

You might also like