# Conditional Statements - Assignments

1. **`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.`**

{% tabs %}
{% tab title="Example 1" %}
**`Enter your salary : 85000`**

**`Output :`**&#x20;

**`Your Bonus : 0`**

**`Your This Month Salary : 85000`**
{% endtab %}

{% tab title="Example 2" %}
**`Enter your salary : 45000`**

**`Output :`**&#x20;

**`Your Bonus : 2250`**

**`Your This Month Salary : 47500`**
{% endtab %}
{% endtabs %}

<details>

<summary><mark style="color:purple;">Solution</mark></summary>

**First, we will take values from the user.**

<mark style="color:green;">**`salary = int(input('Enter your salary : '))`**</mark>&#x20;

**Let us take the bonus and initialize it as 0, as we do not know anything about it.**

<mark style="color:green;">**`bonus = 0`**</mark>

**Now we will use a conditional statement that if Employee's salary is less than 75000, apply a 5 percent bonus.**

<mark style="color:green;">**`if(salary<75000):`**</mark>&#x20;

&#x20;   <mark style="color:green;">**`bonus = salary * 5/100`**</mark>

**Voila!!!**

<mark style="color:green;">**`print(f'Your Bonus : {bonus}')`**</mark>&#x20;

<mark style="color:green;">**`print(f'This Month Salary : {salary})`**</mark>

</details>

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.`**

{% tabs %}
{% tab title="Example 1" %}
**`Number of classes held : 100`**&#x20;

**`Number of classes attended : 50`**&#x20;

**`Attendance Percentage : 50.0%`**

**`You are not allowed to sit in exam.`**
{% endtab %}

{% tab title="Example 2" %}
**`Number of classes held : 100`**&#x20;

**`Number of classes attended : 75`**&#x20;

**`Attendance Percentage : 75.0%`**

**`You are  allowed to sit in exam.`**
{% endtab %}
{% endtabs %}

<details>

<summary><mark style="color:purple;">Solution</mark></summary>

**Let us ask , how many classes were held and how many were attended ?**&#x20;

<mark style="color:green;">**`class_held = int(input('Number of classes held : '))`**</mark>&#x20;

<mark style="color:green;">**`class_attend = int(input('Number of classes attended : '))`**</mark>&#x20;

**Calculate Percentage :**&#x20;

<mark style="color:green;">**`attendance_percentage = class_attend * 100 / class_held`**</mark>&#x20;

<mark style="color:green;">**`print(f'Attendance Percentage : {attendance_percentage}%')`**</mark>

</details>

**`3.Write a program to check whether a person is eligible for voting or not.(voting age >=18)`**

{% tabs %}
{% tab title="Example 1" %}
**`Enter your age : 19`**&#x20;

**`You are allowed to vote`**
{% endtab %}

{% tab title="Example 2 " %}
**`Enter your age : 17`**&#x20;

**`You are not allowed to vote`**
{% endtab %}
{% endtabs %}

<details>

<summary><mark style="color:purple;">Solution</mark> </summary>

**First we will ask age of the voter.**

<mark style="color:green;">**`age = int(input('Enter your age : '))`**</mark>&#x20;

**Check the voter's age :**&#x20;

<mark style="color:green;">**`if(age>=18):`**</mark>&#x20;

&#x20;     <mark style="color:green;">**`print('You are allowed to vote')`**</mark>&#x20;

<mark style="color:green;">**`else:`**</mark>&#x20;

&#x20;     <mark style="color:green;">**`print('You are not eligible to vote.')`**</mark>

</details>

**`4.Write a program to check whether a number entered by user is even or odd.`**&#x20;

{% tabs %}
{% tab title="Example 1" %}
**`Enter the number : 17`**&#x20;

**`17 is odd`**
{% endtab %}

{% tab title="Second Tab" %}
**`Enter the number : 16`**&#x20;

**`16 is even`**


{% endtab %}
{% endtabs %}

<details>

<summary><mark style="color:purple;">Solution</mark></summary>

<mark style="color:green;">**`num = int(input('Enter the number : '))`**</mark>&#x20;

<mark style="color:green;">**`if(num%2==0):`**</mark>&#x20;

&#x20;     <mark style="color:green;">**`print(f'{num} is even')`**</mark>&#x20;

<mark style="color:green;">**`else:`**</mark>&#x20;

&#x20;     <mark style="color:green;">**`print(f'{num} is odd')`**</mark>

</details>

**`5.Write a program to calculate the electricity bill (accept number of unit from user) according to the following criteria :`**&#x20;

| Unit                  | Price                |
| --------------------- | -------------------- |
| **`First 100 Units`** | **`No charge`**      |
| **`Next 100 Units`**  | **`5 Rs Per Unit`**  |
| **`After 200 Units`** | **`10 Rs Per Unit`** |

<details>

<summary><mark style="color:purple;">Solution</mark></summary>

<mark style="color:green;">**`no_of_unit = int(input('Units : '))`**</mark>&#x20;

<mark style="color:green;">**`if(no_of_unit<=100):`**</mark>&#x20;

&#x20;   <mark style="color:green;">**`bill = 0`**</mark>&#x20;

<mark style="color:green;">**`elif(no_of_unit>100 and no_of_unit<=200):`**</mark>&#x20;

&#x20;   <mark style="color:green;">**`bill = (no_of_unit-100) * 5`**</mark>&#x20;

<mark style="color:green;">**`else:`**</mark>&#x20;

&#x20;   <mark style="color:green;">**`bill = 500 + (no_of_unit - 200)*10`**</mark>

<mark style="color:green;">**`print(bill)`**</mark>

</details>

**`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`** |

{% tabs %}
{% tab title="Example" %}
**`Enter the marks : 87`**&#x20;

**`B`**
{% endtab %}
{% endtabs %}

<details>

<summary><mark style="color:purple;">Solution</mark></summary>

<mark style="color:green;">**`marks = int(input('Enter the marks : '))`**</mark>

<mark style="color:green;">**`if(marks>90):`**</mark>&#x20;

&#x20;   <mark style="color:green;">**`print('A')`**</mark>&#x20;

<mark style="color:green;">**`elif(marks>80 and marks<=90):`**</mark>&#x20;

&#x20;   <mark style="color:green;">**`print('B')`**</mark>&#x20;

<mark style="color:green;">**`elif(marks>60 and marks<=80):`**</mark>&#x20;

&#x20;   <mark style="color:green;">**`print('C')`**</mark>&#x20;

<mark style="color:green;">**`else:`**</mark>&#x20;

&#x20;   <mark style="color:green;">**`print('D')`**</mark>

</details>

**`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”`**

<details>

<summary><mark style="color:purple;">Solution</mark></summary>

<mark style="color:green;">**`age = int(input('Enter age : '))`**</mark>&#x20;

<mark style="color:green;">**`if(age>=18 and age<30):`**</mark>&#x20;

&#x20;   <mark style="color:green;">**`gen = input('gender : ')`**</mark>&#x20;

&#x20;   <mark style="color:green;">**`if(gen == 'M'):`**</mark>&#x20;

&#x20;      <mark style="color:green;">**`wage = 700`**</mark>&#x20;

&#x20;  <mark style="color:green;">**`else:`**</mark>&#x20;

&#x20;      <mark style="color:green;">**`wage = 750`**</mark>&#x20;

&#x20;  <mark style="color:green;">**`print(wage)`**</mark>&#x20;

<mark style="color:green;">**`elif(age>=30 and age<=40):`**</mark>&#x20;

&#x20;   <mark style="color:green;">**`gen = input('gender : ')`**</mark>&#x20;

&#x20;   <mark style="color:green;">**`if(gen=='F'):`**</mark>&#x20;

&#x20;      <mark style="color:green;">**`wage = 800`**</mark>&#x20;

&#x20;   <mark style="color:green;">**`else:`**</mark>&#x20;

&#x20;     **` `**<mark style="color:green;">**`wage = 850`**</mark>&#x20;

&#x20;   <mark style="color:green;">**`print(wage)`**</mark>&#x20;

<mark style="color:green;">**`else:`**</mark>&#x20;

&#x20;   <mark style="color:green;">**`print('Enter Appropriate Age.')`**</mark>

</details>

**`8.Accept the number of days from the user and calculate the charge for library according to following :`**&#x20;

| Day                          | Price   |
| ---------------------------- | ------- |
| **`Till five days`**         | **`2`** |
| **`Six to Ten days`**        | **`3`** |
| **`Eleven to Fifteen days`** | **`4`** |
| **`After Fifteen days`**     | **`5`** |

{% tabs %}
{% tab title="Ecample1 " %}
**`Enter the number of days : 16`**&#x20;

**`Library Fees : 50`**
{% endtab %}
{% endtabs %}

<details>

<summary><mark style="color:purple;">Solution</mark></summary>

<mark style="color:green;">**`no_of_days = int(input('Enter the number of days : '))`**</mark>&#x20;

<mark style="color:green;">**`if(no_of_days<=5):`**</mark>&#x20;

&#x20;   <mark style="color:green;">**`fees = no_of_days * 2`**</mark>&#x20;

<mark style="color:green;">**`elif(no_of_days>=6 and no_of_days<=10):`**</mark>&#x20;

&#x20;   <mark style="color:green;">**`fees = 10 + (no_of_days-5)*3`**</mark>&#x20;

<mark style="color:green;">**`elif(no_of_days>=11 and no_of_days<=15):`**</mark>&#x20;

&#x20;   <mark style="color:green;">**`fees = 25 + (no_of_days-10)*4`**</mark>&#x20;

<mark style="color:green;">**`else:`**</mark>&#x20;

&#x20;   <mark style="color:green;">**`fees = 45 + (no_of_days-15) * 5`**</mark>

<mark style="color:green;">**`print(f'Library Fees : {fees}')`**</mark>

</details>

**`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.`**

{% tabs %}
{% tab title="Example 1" %}
**`Enter the year : 2000`**&#x20;

**`Leap Year`**
{% endtab %}

{% tab title="Example 2" %}
**`Enter the year : 2100`**&#x20;

**`Not a Leap Year`**
{% endtab %}
{% endtabs %}

**`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.`**&#x20;

**`Phone Number should match following Criteria :`**&#x20;

1. **`Only numbers , spaces and - are allowed though (so few customers wrote their number with spaces.)`**
2. **`12 digits only.`**
3. **`+ 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 ?`**

{% tabs %}
{% tab title="Example 1" %}
**`number = '+91 - 765 - 181 - 1162'`**

**`Valid Number`**
{% endtab %}

{% tab title="Untitled" %}
**`number = '91 - 7651811162'`**

**`Valid Number`**
{% endtab %}
{% endtabs %}

<details>

<summary><mark style="color:purple;"><strong>Solution</strong></mark></summary>



<mark style="color:green;">**`phone = '+91-765-181-1+62'`**</mark>

<mark style="color:green;">**`if(phone.startswith('+')):`**</mark>&#x20;

&#x20;   <mark style="color:green;">**`phoneno = phone.replace('+','',1)`**</mark>

<mark style="color:green;">**`phoneno = phone.replace('-','').replace(' ','')`**</mark>



<mark style="color:green;">**`if(phoneno.isnumeric()):`**</mark>&#x20;

&#x20;   <mark style="color:green;">**`if(len(phoneno)==12):`**</mark>&#x20;

&#x20;       <mark style="color:green;">**`print('It is a valid Phone number.')`**</mark>&#x20;

&#x20;   <mark style="color:green;">**`else:`**</mark>&#x20;

&#x20;       <mark style="color:green;">**`print('Not a valid phone number, more numbers')`**</mark>&#x20;

<mark style="color:green;">**`else:`**</mark>&#x20;

&#x20;   <mark style="color:green;">**`print('Other characters')`**</mark>

</details>

**`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.`**



<details>

<summary><mark style="color:purple;">Solution</mark></summary>

<mark style="color:green;">**`pin = '34893794#@#'`**</mark>

<mark style="color:green;">**`if(len(pin)>=10):`**</mark>&#x20;

&#x20;   <mark style="color:green;">**`if('&' in pin or '@' in pin or'# ' in pin):`**</mark>&#x20;

&#x20;       <mark style="color:green;">**`p = pin.replace('&','').replace('@','').replace('#','')`**</mark>&#x20;

&#x20;       <mark style="color:green;">**`if(p.isnumeric()):`**</mark>

&#x20;           <mark style="color:green;">**`print('Valid Pin')`**</mark>&#x20;

&#x20;       <mark style="color:green;">**`else:`**</mark>&#x20;

&#x20;           <mark style="color:green;">**`print('Unacceptable Characters')`**</mark>&#x20;

&#x20;    <mark style="color:green;">**`else:`**</mark>&#x20;

&#x20;        <mark style="color:green;">**`print('Required Characters[&@#] are not in pin')`**</mark>&#x20;

<mark style="color:green;">**`else:`**</mark>&#x20;

&#x20;    <mark style="color:green;">**`print('Less characters in Pin')`**</mark>

</details>

**`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.`**

<details>

<summary><mark style="color:purple;">Solution</mark></summary>

```python
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.")

```

</details>

**`Write a program to find the largest of three numbers.`**



**Write a program to check whether a given string is a palindrome or not.**

<details>

<summary>Solution</summary>

```python
string = input("Enter a string: ")

if string == string[::-1]:
    print("The string is a palindrome")
else:
    print("The string is not a palindrome")

```

</details>

**Write a program to check whether a given character is a vowel or a consonant.**

<details>

<summary>Solution</summary>

```python
char = input("Enter a character: ")

if char in "AEIOUaeiou":
    print(char, "is a vowel")
else:
    print(char, "is a consonant")

```

</details>

**Write a program to check whether a given string is a valid email address&#x20;**_**(i.e., it contains exactly one "@" symbol and at least one "." after the "@").**_

<details>

<summary>Solution</summary>

```python
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")

```

</details>
