DEV Community

Guru prasanna
Guru prasanna

Posted on

Python - Level : 2 Tasks

Level:2 Tasks

1) 7, 10, 8, 11, 9, 12, 10

# 7,10,8,11,9,12,10

no = 7

count = 0

while count < 7:
    print(no, end=",")
    if count % 2 == 0:  
        no += 3
    else:  
        no -= 2
    count += 1
Enter fullscreen mode Exit fullscreen mode

Output:

7,10,8,11,9,12,10,
Enter fullscreen mode Exit fullscreen mode

2) 36, 34, 30, 28, 24, 22

num = 36

diff=2

while num >= 22:
    print(num, end=",")
    if diff==2 :
        num -= diff
        diff=4
    else:
        num -= diff
        diff=2
Enter fullscreen mode Exit fullscreen mode

Output:

36,34,30,28,24,22,
Enter fullscreen mode Exit fullscreen mode

3) 22, 21, 23, 22, 24, 23


# 22, 21, 23, 22, 24, 23

num = 22

diff=1

while num <= 24:
    print(num, end=",")
    if diff==1 :
        num -= diff
        diff+=1
    else:
        num += diff
        diff=1


Enter fullscreen mode Exit fullscreen mode

Output:

22,21,23,22,24,23,
Enter fullscreen mode Exit fullscreen mode

4) 53, 53, 40, 40, 27, 27

# 53, 53, 40, 40, 27, 27

num = 53

diff=13

while num >= 27:
    print(num,num, end=",")
    if diff==13 :
        num -= diff

    else:
        break
Enter fullscreen mode Exit fullscreen mode

5) 21, 9, 21, 11, 21, 13, 21


fixed_number = 21
no = 9

count = 0

while count < 7:
    if count % 2 == 0:  
        print(fixed_number, end=', ')
    else:  
        print(no, end=', ')
        no += 2  
    count += 1

Enter fullscreen mode Exit fullscreen mode

Output:

21, 9, 21, 11, 21, 13, 21, 
Enter fullscreen mode Exit fullscreen mode

6) 3, 4, 7, 8, 11, 12

# 3, 4, 7, 8, 11, 12
no = 3  
count = 0  


while count < 6:
    if count % 2 == 0:  
        print(no, end=', ')
    else:  
        print(no+ 1, end=', ')
        no += 4 
    count += 1

Enter fullscreen mode Exit fullscreen mode

Output:

3, 4, 7, 8, 11, 12,
Enter fullscreen mode Exit fullscreen mode

7) 14, 28, 20, 40, 32, 64


no = 14   
difference = 8 
count = 0 


while count < 6:
    if count % 2 == 0: 
        print(no, end=', ')
        no *= 2 
    else: 

        print(no, end=', ')
        no-=8  
    count += 1

Enter fullscreen mode Exit fullscreen mode

Output:

14, 28, 20, 40, 32, 64, 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)