A company decided to give bonus of 5% to employees if his/her salary is less than 75000. Ask user for their salary and calculate his bonus and this month salary.
Enter your salary : 85000
Output :
Your Bonus : 0
Your This Month Salary : 85000
Enter your salary : 45000
Output :
Your Bonus : 2250
Your This Month Salary : 47500
Solution
First, we will take values from the user.
salary = int(input('Enter your salary : '))
Let us take the bonus and initialize it as 0, as we do not know anything about it.
bonus = 0
Now we will use a conditional statement that if Employee's salary is less than 75000, apply a 5 percent bonus.
if(salary<75000):
bonus = salary * 5/100
Voila!!!
print(f'Your Bonus : {bonus}')
print(f'This Month Salary : {salary})
2. A student will not be allowed to sit in exam if his attendance is less than 75 percent.Ask how many classes were held and how many classes were attended and calculate his percentage.
Number of classes held : 100
Number of classes attended : 50
Attendance Percentage : 50.0%
You are not allowed to sit in exam.
Number of classes held : 100
Number of classes attended : 75
Attendance Percentage : 75.0%
You are allowed to sit in exam.
Solution
Let us ask , how many classes were held and how many were attended ?
class_held = int(input('Number of classes held : '))
class_attend = int(input('Number of classes attended : '))
3.Write a program to check whether a person is eligible for voting or not.(voting age >=18)
Enter your age : 19
You are allowed to vote
Enter your age : 17
You are not allowed to vote
Solution
First we will ask age of the voter.
age = int(input('Enter your age : '))
Check the voter's age :
if(age>=18):
print('You are allowed to vote')
else:
print('You are not eligible to vote.')
4.Write a program to check whether a number entered by user is even or odd.
Enter the number : 17
17 is odd
Enter the number : 16
16 is even
Solution
num = int(input('Enter the number : '))
if(num%2==0):
print(f'{num} is even')
else:
print(f'{num} is odd')
5.Write a program to calculate the electricity bill (accept number of unit from user) according to the following criteria :
Unit
Price
First 100 Units
No charge
Next 100 Units
5 Rs Per Unit
After 200 Units
10 Rs Per Unit
Solution
no_of_unit = int(input('Units : '))
if(no_of_unit<=100):
bill = 0
elif(no_of_unit>100 and no_of_unit<=200):
bill = (no_of_unit-100) * 5
else:
bill = 500 + (no_of_unit - 200)*10
print(bill)
6.Write a program to accept percentage from the user and display the grade according to the following criteria:
Mark
Grade
>=90
A
>=80 and <90
B
>=60 and <80
C
<60
D
Enter the marks : 87
B
Solution
marks = int(input('Enter the marks : '))
if(marks>90):
print('A')
elif(marks>80 and marks<=90):
print('B')
elif(marks>60 and marks<=80):
print('C')
else:
print('D')
7.Accept the age, sex (‘M’, ‘F’), number of days and display the wages accordingly.
Age
Sex
Wage
>=18 and <30
M
700
F
750
>=30 and <=40
M
800
F
850
If age does not fall in any range then display the following message: “Enter appropriate age”
Solution
age = int(input('Enter age : '))
if(age>=18 and age<30):
gen = input('gender : ')
if(gen == 'M'):
wage = 700
else:
wage = 750
print(wage)
elif(age>=30 and age<=40):
gen = input('gender : ')
if(gen=='F'):
wage = 800
else:
wage = 850
print(wage)
else:
print('Enter Appropriate Age.')
8.Accept the number of days from the user and calculate the charge for library according to following :
Day
Price
Till five days
2
Six to Ten days
3
Eleven to Fifteen days
4
After Fifteen days
5
Enter the number of days : 16
Library Fees : 50
Solution
no_of_days = int(input('Enter the number of days : '))
if(no_of_days<=5):
fees = no_of_days * 2
elif(no_of_days>=6 and no_of_days<=10):
fees = 10 + (no_of_days-5)*3
elif(no_of_days>=11 and no_of_days<=15):
fees = 25 + (no_of_days-10)*4
else:
fees = 45 + (no_of_days-15) * 5
print(f'Library Fees : {fees}')
9.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.
Enter the year : 2000
Leap Year
Enter the year : 2100
Not a Leap Year
You work in a company and you are given customers number, But many customers filled wrong data, so you need to check if the number given by customer is right or not.
Phone Number should match following Criteria :
Only numbers , spaces and - are allowed though (so few customers wrote their number with spaces.)
12 digits only.
+ is allowed , though only at beginning of the number.
If it matches all the three criterias , Print it is a valid number else Print it is not a valid number with the reason why ?
number = '+91 - 765 - 181 - 1162'
Valid Number
number = '91 - 7651811162'
Valid Number
Solution
phone = '+91-765-181-1+62'
if(phone.startswith('+')):
phoneno = phone.replace('+','',1)
phoneno = phone.replace('-','').replace(' ','')
if(phoneno.isnumeric()):
if(len(phoneno)==12):
print('It is a valid Phone number.')
else:
print('Not a valid phone number, more numbers')
else:
print('Other characters')
You need to set a pin for your computer but there are certain rules that your pin should have to be a valid pin for your machine.
1.It should have only numbers
2.It should have any of these three symbols [&@#]
3.It should have more than 10 characters.
Solution
pin = '34893794#@#'
if(len(pin)>=10):
if('&' in pin or '@' in pin or'# ' in pin):
p = pin.replace('&','').replace('@','').replace('#','')
if(p.isnumeric()):
print('Valid Pin')
else:
print('Unacceptable Characters')
else:
print('Required Characters[&@#] are not in pin')
else:
print('Less characters in Pin')
Create a Sign up and login portal where take values like userid and password from user and sign up and then ask him to login. if credetials match print Welcome to python session else print incorrect details.
Additioal : print reason why he could not login (was user id wrong or password wrong ? )
Given two numbers, determine which one is larger.
Solution
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
if num1 > num2:
print("The first number is larger.")
elif num1 < num2:
print("The second number is larger.")
else:
print("Both numbers are equal.")
Write a program to find the largest of three numbers.
Write a program to check whether a given string is a palindrome or not.
Solution
string = input("Enter a string: ")
if string == string[::-1]:
print("The string is a palindrome")
else:
print("The string is not a palindrome")
Write a program to check whether a given character is a vowel or a consonant.
Solution
char = input("Enter a character: ")
if char in "AEIOUaeiou":
print(char, "is a vowel")
else:
print(char, "is a consonant")
Write a program to check whether a given string is a valid email address (i.e., it contains exactly one "@" symbol and at least one "." after the "@").
Solution
email = input("Enter an email address: ")
if email.count("@") == 1 and "." in email[email.index("@"):]:
print("The email address is valid")
else:
print("The email address is not valid")