Tuesday, July 27, 2021

Python - day15 - if else

 Session 14:  7/15/2021

Review
<syntax2>
if <condition>:
  st1
  st2
else:
  st3
  st4



a=int(input("Enter first number"))
b=int(input("Please enter 2nd number"))

if a>b:
  print("A is greater then B")
else:
  print("A is less than B"


====================================

When will you go for if elif else?
When we have more than 2 options to choose from based on condition, we will use if elif else.
In this case, if the condition is true, it will execute the statement and skips the rest of the statement.

On the other hand, if if condition is false, the control will go to elif part and executes and else part skips. but if elif is false, then executes the else part.



Write a python program to accept one letter and check if it is vowel or not?

Step1: Design with o/p
----------------------

o/p
-------------------------
enter your letter:
user enters a, store a into a variable called letter: a
return: it is vowel.
Enter your letter:
user enter b, store b into variable called letter: b
return: its not a vowel.

coding
--------
letter=input("Enter your letter")
if letter=='a':
  print("It is a vowel")
elif letter=='e':
  print("It is a vowel")
elif letter=='i':
  print("It is a vowel')
elif letter=='o':
  print("It is a vowel')
elif letter=='u':
  print("It is a vowel")
else
  print("It is not a vowel")

When you run this code
input - enter ur letter
user enters a
a=a
condition is true, the it is vowel and rest of the condition is skips.

run it again
user enters b
b=a -> false , skips
b=e and keep checking and it will go to else part and returns the value
it is not vowel.

In this code we have 6 conditions, one for vower and other for consonent

Task1:
implement above program by using if else?

letter=input("Enter a letter")
if(letter=='a'|'e'|'i'|'o'|'u'):
  print("The letter is vowel")
else:
  print("The letter is not a vowel"


task2
Implement above vowel program by using if else and which should handle casesensitive?

Step1: Design with 0/p
=======================

le=input("Enter a letter")
if (le=='A' or le='a' or le=='E' or le=='e' or le=='I' or le=='i' or le=='o' or le=='O' or le=='U' or le=='u'):
  print(le, "is a vowel")
else
  print(le, "is not a consonant")


Task3
-----
Write a python program to accept student 3 subject marks and display the result?

Step1: Design with O/P
----------------------

o/p
----------
Enter m1 marks: 60
enter m2 marks: 70
enter m3 marks: 80

m1+m2+m3 = 60+70+80 = totmarks
totmarks/3 -> avg marks

Now display result
result can be vary..
avgmarks=70
1. fail: if > 35 in any one subject
2. First division: if >=60
3. 2nd division: if avgmarks is between 50-59
4. 3rd divison: if avg marks is in between 35 to 49

so output form above command is

Result is: Fist division.



Review and complete these tasks before tomorrow class.


----------------------------------

task4:
implement above program using 6 subjects.

task5:
Accept person's age display person status?

Step1: Design with o/p
----------------------

1. if age>=59
print senior citizen

2. if age is in between 25 to 57
- working citizen
3. if age is in between 16 and 24
- college students
4. if age is between 4-15
  - school kids
5. if kids are in between 1-3
  playing kids
6. invalid selection


o/p
---------------
Please enter your age:
user enters 59
sore 59 into a variable 'age' so age=50

print:
you are senior citizen.




No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...