# Practice Problems - Loops

Example 1:

```python
#While Loop Statement
 #A while loop statement in Python programming language repeatedly executes a target statement as long as a given condition is true.
 #Syntax of while loop :-
   #while expression:
     #statement(s)
#Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true.
# Example:-
c = 1
while c <= 10 :
    print("Hello World!")
    c = c + 1
print("Python is Awesome")
```

Hello World!

Hello World!

Hello World!

Hello World!

Hello World!

Hello World!

Hello World!

Hello World!

Hello World!

Hello World!

Python is Awesome

Example 2:

```python
count = 0
while (count < 9):
    print("The count is:",count)
    count = count + 1
print("Good bye!")
```

The count is: 0

The count is: 1

The count is: 2

The count is: 3

The count is: 4

The count is: 5

The count is: 6

The count is: 7

The count is: 8

Good bye!

Example 3:

```python
c = 1
while c <= 100 :
    print(c,end=', ')
    c = c + 1
```

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,

Example 4:

```python
my_string = "Python"
i = "i"
while i in my_string:
    print(i, end =" ")
#these code gives no output why?
```

```python
i = 0
while i < 3: 
    print(i) 
    i += 1
else: 
    print(0) 
```

0

1

2

0

Example 5:

```python
i = 1
while True: 
    if i % 7 == 0: 
        break
    print(i) 
    i += 1
```

1

2

3

4

5

6

Example 6:

```python
i = 1
while True: 
    if i % 3 == 0: 
        break
    print(i) 
    i + = 1  #SyntaxError, there shouldn’t be a space between + and = in +=.
```

File "", line 6 i + = 1 ^

Example 7:

```python
c = 10
while c >= 1 :
    print(c,end='\t')
    c = c - 1
```

10   9   8   7   6   5   4   3   2   1

Example 8:

```python
cars = ["Aston", "Audi", "McLaren"] 
i = 0
while (i < len(cars)): 
    print(cars[i]) 
    i += 1
```

Aston

Audi

McLaren

Example 9:

```python
word="anaconda"
pos=0 
while pos < len(word) :
    print (word[pos])
    pos+=1 
```

a

n

a

c

o

n

d

a

Example 10:

```python
i=0
while i < 10:
    print(i)
    i += 2
```

0

2

4

6

8

Example 11:

```python
num = int(input("Enter number : "))
c = 1
while c <= 10 :
    print("{:5}\tx{:5}\t={:5}".format(num,c,num*c))
    c = c + 1
```

Enter number : 4

4 x 1 = 4

4 x 2 = 8

4 x 3 = 12

4 x 4 = 16

4 x 5 = 20

4 x 6 = 24

4 x 7 = 28

4 x 8 = 32

4 x 9 = 36

4 x 10 = 40

Example 12:

```python
from random import randrange
while input("Do want to roll again : "):
    print("Score : ",randrange(1,7))
```

Do want to roll again : 5

Score : 3

Do want to roll again : 2

Score : 2&#x20;

Do want to roll again :

Example 13:

```python
num = int(input("Enter a Number : "))

if num <= 1 :
    
    print("Not Prime")
    print("Error!This is not valid input to check prime")
    exit(0)
    
elif num <= 3 :
    
    print("Prime")
    exit(0)
    
check = 2
while check <= num // 2 + 1 :
    k = num % check 
    if k == 0 :
        print("Not Prime")
        break
    elif check == num //2 + 1 :
        print("Prime")
    check = check + 1
```

Enter a Number : 55

Not Prime

Example 14:

```python
#Fibbonacci Series using While Loop
num = int(input("Enter the no of terms"))
x = 0
y = 1
a = 0
while a < num:
    z = x + y
    x = y
    y = z
    a = a + 1
    print(z)
```

Enter the no of terms5

1

2

3

5

8

Example 15:

```python
#Rock, Paper, Scissor game using while loop(2 Players)
p1 = [ ('r','s'),('p','r'),('s','p')]
from getpass import getpass
while True :
    ch1 = getpass("Player1: ")
    ch2 = getpass("Player2: ")
    if ch1 == ch2 : 
        print("Match Tie")
    elif (ch1,ch2) in p1 :
        print("Player1 is the Winner")
    else :
        print("Player2 is the Winner")
    
    ch = input("Do want to continue (y/n) : ").strip().lower()
    if ch == 'y' or ch == 'yes' :
        continue
    else :
        break
```

Player1: p&#x20;

Player2: r&#x20;

Player1 is the Winner&#x20;

Do want to continue (y/n) : n

Example 16:

```python
##Rock, Paper, Scissor game using while loop(1 Player)
p1 = [ ('r','s'),('p','r'),('s','p')]
from random import choice
while True :
    ch1 = choice(['r','p','s'])
    ch2 = input("Player2: ")
    if ch1 == ch2 : 
        print("Match Tie")
    elif (ch1,ch2) in p1 :
        print("Computer Choice was : ",ch1)
        print("Player Choice was : ",ch2)
        print("Player1 is the Winner")
    else :
        print("Computer Choice was : ",ch1)
        print("Player Choice was : ",ch2)
        print("Player2 is the Winner")  
    ch = input("Do want to continue (y/n) : ").strip().lower()
    if ch == 'y' or ch == 'yes' :
        continue
    else :
        break
```

Player2: r&#x20;

Computer Choice was : s

Player Choice was : r

Player2 is the Winner

Do want to continue (y/n) : n


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gyansetu-python.gitbook.io/python-programming/conditional-statements-and-loops/loops-in-python.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
