loop statement job

Operation:

1. Use while loop to input 1 2 3 4 5 6 8 9 10 (without 7)

 

n = 1
while n < 11:
    if n == 7:
        pass
    else:
        print(n)
    n = n + 1
 print ( ' end ' )

 

2. Find the sum of all numbers from 1 to 100

 

n = 1
s = 0
while n < 101:
    s = s + n
    n = n + 1
print(s)

 

3. Output all odd numbers from 1-100

 

n = 1
while n <101:
    print(n)
    n = n + 2
 print ( ' end ' )

 

 

n = 1
while n <101:
    temp = n % 2
    if temp == 0:
        pass
    else:
        print(n)
    n = n + 2
 print ( ' end ' )

 

4. Output all even numbers from 1-100

 

n = 1
while n < 101:
    temp = n % 2
    if temp == 0:
        print(n)
    else:
        pass
    n = n + 1
print('结束')

 

5. Find the sum of all the numbers 1-2+3-4+5...99

 

n = 1
s = 0
while n < 101:
    temp = n % 2
    if temp == 0:
        s = s - n
    else:
        s = s + n
    n = n + 1
print(n)

 

6. User login (three chances to retry)

 

 

 

 

Infinite loop:

Pay attention to the difference between spaces in the last print.

count = 0
 while count < 10 :
  print ( ' Continue running ' )
  count = count + 1
 print ( ' End run ')

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324979091&siteId=291194637