Simple while loop exercise (python)

Record simple while loop exercises

Topic 1:

Use the while loop to output 1 2 3 4 5 6 8 9 10
count = 0

while count < 10:

  count+=1

  if(count != 7):

​    print(count)

Problem solving ideas:

This question is relatively simple, that is, print the number of 1-10 except 7

  1. Let's make it a little simpler and print out 1-10 in a cycle

    count = 0
    
    while count <= 10:
    
      print(count)
    
      count += 1
    

    Note: novices should pay attention to count += 1, in order to make the program tend to end

    2. Then remove 7 and do not print, that is, print as long as it is not 7, so add a conditional judgment

    if(count != 7):
            print(count)
    

    3. The final code is

    count = 0
    
    while count < 10:
    
      count+=1
    
      if(count != 7):
    
    ​   print(count)
    

Topic 2:

Find the sum of all numbers from 1 to 100
sum = 0

i = 1

while i <= 100:

  sum += i

  i+=1

print(sum)

Problem solving ideas:

1. The topic is to find the sum of 1-100. We can narrow the scope. Let's find the sum of 1 and 2 first

a = 1
b = 2
sum = a + b

2. Let's find the sum of 1-3 again

a = 1
b = 2
c = 3
sum = a + b + c

3. However, if there are a lot of numbers, we cannot assign values one by one, so we use the loop to assign values, and we still use sum to record the sum

i = 1       # Use i as the initial variable
sum = 0       # sum starts at 0 
while i <= 3:         # Set cycle conditions
    sum += i     # sum accumulates the value of i one by one
    i += 1         # i gradually tends to the end of cycle condition

tip: sum is like a big house, i like a porter who brings in the numbers 1-3 one by one and adds them to sum

4. Then we can change it into the sum of 1-100 numbers, and finally print the result. The final code is:

i = 1
sum = 0
while i <= 100:
    sum += i
    i += 1
print(sum)

Topic 3:

Output all odd numbers in 1-100
i = 1

while i <= 100:

  if(i%2 != 0):

​    print(i)

  i+=1

1. Let's narrow it down and output the number of 1-5 first in a circular way

i = 1
while i <= 5:
    print(i)
    i += 1

2. Then we add a conditional judgment to filter out odd numbers. The difference between odd numbers and even numbers is whether they can be divided by 2 integers, that is, if the remainder is 0, it is even, and if the remainder is not 0, it is odd. We only need odd numbers, so we add a judgment condition

 if(i%2 != 0):
         print(i)

3. Then you can output odd numbers of 1-5

i = 1
while i <= 5:
    if(i%2 != 0):
         print(i)
    i += 1

4. Finally, just change the cycle condition to within 1-100

i = 1
while i <= 100:
    if(i%2 != 0):
         print(i)
    i += 1

Topic 4:

Output all even numbers in 1-100
i = 1
while i <= 100:
  if(i%2 == 0):
​    print(i)
  i+=1

This topic is very similar to the last one. The idea of solving the problem is the same

Topic 5:

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

Scheme I

sum = 0
i = 1
while i <= 99:
    if(i%2 == 0):
        tmp = -i
    else:
        tmp = i   
    sum += tmp
    i+=1
print(sum)

Problem solving ideas:

1. Let's narrow down the range first and calculate the number from 1 to 5. It's not difficult to find out through observation that the even number here is preceded by a negative sign, so we should find out all the even numbers through conditional judgment, add a negative sign, and then add them to sum. For odd numbers, there is no need to add a negative sign

if(i%2 == 0):
    tmp = -i
 else:
	 tmp = i

tip: the temporary variable tmp is used here so as not to affect the loop, because i is the key variable of the loop. We only want the positive or negative value of i, but if i is negative, it will affect the loop, so we should ensure that i executes normally

2. After judging the parity, you can add it to sum

if(i%2 == 0):
	tmp = -i
else:
	tmp = i
sum += tmp

3. Finally put on the loop

sum = 0
i = 1 
while i <= 5:
     if(i%2 == 0):
          tmp = -i
     else:
          tmp = i
     sum += tmp
     

4. Then change the cycle condition to 1-99

sum = 0
i = 1
while i <= 99:
    if(i%2 == 0):
        tmp = -i
    else:
        tmp = i   
    sum += tmp
    i+=1
print(sum)

Scheme II

Problem solving ideas:

We can sum up separately, first find the odd sum, and then calculate the even sum. Because even numbers are negative, odd and plus even sum are equivalent to odd and - even sum

1. Let's narrow the scope first, calculate 1-5 first, and define two variables: odd and even

sum_odd = 0
sum_even = 0

2. We need a condition to judge whether i is odd or even. If it is odd, it will be added to sum_ In odd, if it is even, add it to sum_ Evenli

if(i%2 != 0):
     sum_odd += i
elif(i%2 == 0):
     sum_even += i

3. The last set of cycles, then odd and even sum, and finally output the result

sum_odd = 0
sum_even = 0
i = 1
while i <= 5:
     if(i%2 != 0):
          sum_odd += i
     elif(i%2 == 0):
          sum_even += i
     i+=1
sum = sum_odd - sum_even
print(sum)

4. Finally, change the cycle condition. The final code is:

sum_odd = 0
sum_even = 0
i = 1
while i <= 99:
     if(i%2 != 0):
          sum_odd += i
     elif(i%2 == 0):
          sum_even += i
     i+=1
sum = sum_odd - sum_even
print(sum)

Tags: Python

Posted by veryconscious on Wed, 25 May 2022 11:47:58 +0300