Examples of while loop in Python programming
All Contributions
Write python program that computes the sum of the squares between 1 and n where n is given by the user. Use while loop to ensure that the value of n is strictly positive.
import math
summ=0
n=int(input("enter n:"))
i=1
while i<=n:
summ+=math.sqrt(i)
i+=1
print("sum of squares is: ",summ)
output:
enter n:4
sum of squares is: 6.146264369941973
Write python program that computes n^p where n and p are two numbers given by the user
n=int(input('enter the number'))
p=int(input('enter the power'))
counter=1
result=1
while counter<=p:
result*=n #same as result=result*n
counter+=1 #same counter=counter+1
print("result is :",result)
write python program to print the even numbers between 1 to 100
c=1
while c<=100:
if c % 2==0:
print(c)
c = c + 1
print("done")
guess game in python using while loop
correctNumber=int(input("please enter the number "))
running=True
while running==True:
guess=int(input("enter your guess:"))
if guess==correctNumber:
print("congratulations,, you guessed it!")
running=False
elif guess>correctNumber:
print("wrong number, your number is bigger than correct number")
elif guess
total contributions (14)
output:
cuckoo!cuckoo!cuckoo!cuckoo!cuckoo!cuckoo!cuckoo!cuckoo!cuckoo!cuckoo!