Practice questions on Decide if/else

Level 1

A company decided to give bonus of 5% to employee if his/her year of service is more than 5 years.
Ask user for their salary and year of service and print the net bonus amount.
print "Enter salary"
salary = input()
print "Enter year of service"
yos = input()
if yos>5:
  print "Bonus is",.05*salary
  print "No bonus"

Take values of length and breadth of a rectangle from user and check if it is square or not.
print "Enter length"
length = input()
print "Enter breadth"
breadth = input()
if length == breadth:
  print "Yes, it is square"
  print "No, it is only Rectangle"

Take two int values from user and print greatest among them.
print "Enter first number"
first = input()
print "Enter second number"
second = input()
if first>second:
  print "Greatest is",first
elif second>first:
  print "Greatest is",second
  print "Both are equal"

A shop will give discount of 10% if the cost of purchased quantity is more than 1000.
Ask user for quantity
Suppose, one unit will cost 100.
Judge and print total cost for user.
print "Enter quantity"
quantity = input()
if quantity*100 > 1000:
  print "Cost is",((quantity*100)-(.1*quantity*100))
  print "Cost is",quantity*100

A school has following rules for grading system:
a. Below 25 - F
b. 25 to 45 - E
c. 45 to 50 - D
d. 50 to 60 - C
e. 60 to 80 - B
f. Above 80 - A
Ask user to enter marks and print the corresponding grade.
print "Enter marks"
marks = input()
if marks<25:
  print "F"
elif marks>=25 and marks<45:
  print "E"
elif marks>=45 and marks<50:
  print "D"
elif marks>=50 and marks<60:
  print "C"
elif marks>=60 and marks<80:
  print "B"
  print "A"

Take input of age of 3 people by user and determine oldest and youngest among them.
print "Enter first age"
first = input()
print "Enter second age"
second = input()
print "third age"
third = input()

if first >= second and first >= third:
  print "Oldest is",first
elif second >= first and second >= third:
  print "Oldest is",second
elif third >= first and third >= second:
  print "Oldest is",third
  print "All are equal"	

Write a program to print absolute vlaue of a number entered by user. E.g.-
INPUT: 1        OUTPUT: 1
INPUT: -1        OUTPUT: 1
print "Enter a number"
number = input()
if number<0:
  print number*-1
  print number

A student will not be allowed to sit in exam if his/her attendence is less than 75%.
Take following input from user
Number of classes held
Number of classes attended.
And print
percentage of class attended
Is student is allowed to sit in exam or not.
print "Number of classes held"
noh = input()
print "Number of classes attended"
noa = input()
atten = (noa/float(noh))*100
print "Attendence is",atten
if atten >= 75:
  print "You are allowed to sit in exam"
  print "Sorry, you are not allowed. Attend more classes from next time."

Modify the above question to allow student to sit if he/she has medical cause. Ask user if he/she has medical cause or not ( 'Y' or 'N' ) and print accordingly.
#Hint only
#Do it yourself
if medical_cause == 'Y':
  print "You are allowed"
  if atten>=75:
    print "Allowed"
    print "Not allowed"

Level 2

Write a program to check if a year is leap year or not.
If a year is divisible by 4 then it is leap year but if the year is century year like 2000, 1900, 2100 then it must be divisible by 400.
Use % ( modulus ) operator.

Ask user to enter age, sex ( M or F ), marital status ( Y or N ) and then using following rules print their place of service.
if employee is female, then she will work only in urban areas.

if employee is a male and age is in between 20 to 40 then he may work in anywhere

if employee is male and age is in between 40 t0 60 then he will work in urban areas only.

And any other input of age should print "ERROR".
print "Enter age"
age = input()
print "SEX? (M or F)"
sex = raw_input()
print "MARRIED? (Y or N)"
marry = raw_input()
if sex == "F" and age>=20 and age<=60:
  print "Urban areas only"
elif sex == "M" and age>=20 and age<=40:
  print "You can work anywhere"
elif sex == "M" and age>40 and age<=60:
  print "Urban areas only"
  print "ERROR"

A 4 digit number is entered through keyboard. Write a program to print a new number with digits reversed as of orignal one. E.g.-
INPUT : 1234        OUTPUT : 4321
INPUT : 5982        OUTPUT : 2895
print "Enter number" number = input() #seperating digits of the number #1234/1000 = 1 #1234%1000 = 234 first_number = number/1000 rem = number%1000 #234/100 = 2 #234%100 = 34 second_number = rem/100 rem = rem%100 #34/10 = 3 #34%10 = 4 third_number = rem/10 fourth_number = rem%10 #creating new number with digits reversed #1234 = (1*1000)+(2*100)+(3*10)+4 i.e., 1000+200+30+4 new_number = (fourth_number*1000)+(third_number*100)+(second_number*10)+(first_number) print new_number

Level 3

Ask Yours
Post Yours