## सोमवार, 15 जनवरी 2018

### Learn Python in Hindi - Loop Examples

नीचे लूप्स का पहला उदाहरण दिया गया है

# This program asks user to input a integer number
# It sums all the integers from 1 to the user input
# The for loop runs the same program several times

# for Loop
for m in range(0,3):

# User input
x = int(input("Input a number : "))

# Variables
sum = 0
counter = 1

# while Loop
while counter &lt;= x :
sum = sum + counter
counter = counter + 1

# Print is outside of while loop but inside for Loop
print("Sum of all numbers from 1 to %d is : %d" % (x,sum))

नीचे लूप्स का दूसरा उदाहरण दिया गया है

# This program generates a random number between 0 to 20
# It aks user guess the number
# It allows the user 5 attempts
# Every incorrect attempt is alerted by a message
# Hinting if the answer is a bigger or a smaller

# import random module
import random

# randint function
x = random.randint(0,20)

# variables
guess = 0
attempts = 0

# loop
while(attempts &lt; 5):
#   waiting for User input
guess = int(input("Enter your guess (from 0 to 20): "))

if guess &gt; x:
print("You entered a bigger number")
elif guess &lt; x:
print("You entered a smaller number")
elif guess == x:
break
#   loop increment
attempts = attempts + 1
# End of while loop
else:
print("\nYou have exhausted 5 attempts, Better luck next time !!!")