While Loop, MATCH CASE in Python
While Loop, MATCH CASE in Python
Python does not have built-in functionality to explicitly create a do while loop like other
languages. But it is possible to emulate a do while loop in Python.
Python doesn't have do-while loop. But we can create a program like this.
The do while loop is used to check condition after executing the statement. It is like
while loop but it is executed at least once.
Syntax while if
while True:
# statement (s)
If not condition:
break;
i=1
while True:
print(i)
i=i+1
if(i > 5):
break
i = int(input("enter no 1 to 5 = "))
while True:
print(i)
i=i+1
if(i > 5):
break
#The code will run at least one time, asking for user input.
secret_word = "python"
counter = 0
while True:
word = input("Enter the secret word: ").lower()
counter = counter + 1
if word == secret_word:
break
if word != secret_word and counter > 7:
input("you lost")
break
#: take multiple input from user until press y / n
while True:
a = int(input("Enter no.: "))
print("square root is " , a*a)
reply = input('do you want to continue (y/n): ')
if reply == 'n': break
Method 1) If-elif-else
lang = input("What's the programming language you want to learn?- JavaScript, Python, PHP, Solidity,
Java== ")
if lang == "JavaScript":
print("You can become a web developer.")
Method 2) match-case
#EXAMPLE2: An example of a switch statement written with the match case syntax is shown below. It
is a program that prints what you can become when you learn various programming languages:
lang = input("What's the programming language you want to learn?- JavaScript, Python, PHP, Solidity,
Java== ")
match lang:
case "JavaScript":
print("You can become a web developer.")
case "Python":
print("You can become a Data Scientist")
case "PHP":
print("You can become a backend developer")
case "Solidity":
print("You can become a Blockchain developer")
case "Java":
print("You can become a mobile app developer")
case _:
print("The language doesn't matter, what matters is solving problems.")
#example3:
num1 = int(input("enter ist no. "))
num2 = int(input("enter iind no. "))
choice = int(input("1-addition, 2-subtraction 3-multiplication, 4-division "))
match choice:
case 1:
print("THE ADDITION OP IS",num1+num2)
case 2:
print("THE ADDITION OP IS",num1-num2)
case 3:
print("THE ADDITION OP IS",num1*num2)
case 4:
print("THE ADDITION OP IS",num1/num2)
case _:
print(" SORRY WRONG OPTIONS")
a=int(input("entre a no"))
b=1
c=1
while b<=a:
c=c*b
b+=1
print("factorial",c)
a=int(input("entre"))
b=int(input(" entre"))
while b<=a:
if b%2==0:
print("even",b)
else:
print("odd",b)
b=b+1
HOMEWORK