This is the 5th day of my participation in the August More Text Challenge
Title 1:
Use the while loop to print 1, 2, 3, 4, 5, 6, 8, 9, 10
count = 0 while count < 10: count+=1 if(count ! = 7) : print (count)Copy the code
Answer:
This is the easy one, just print the numbers from 1 to 10 except for 7
-
Let’s make it a little bit simpler and print out a loop from 1 to 10
count = 0 while count <= 10: print(count) count += 1 Copy the code
Note: beginners should be careful not to drop count += 1 in order to make the program close
2. Then remove the 7, do not print, as long as it is not 7 print, so add a conditional judgment
if(count ! = 7): print(count)Copy the code
3. The final code is
count = 0 while count < 10: count+=1 if(count ! = 7) : print (count)Copy the code
Topic 2:
Take the sum of all numbers from 1 to 100
sum = 0 i = 1 while i <= 100: sum += i i+=1 print(sum)
Copy the code
Answer:
1. They want to sum 1-100, so we can narrow it down a little bit. Let’s sum 1 and 2 first
a = 1 b = 2 sum = a + b
Copy the code
2. Let’s take the sum of 1-3 again
a = 1 b = 2 c = 3 sum = a + b + c
Copy the code
3. However, if we have a lot of numbers, we can’t assign them one by one, so we use a loop to do the assignment. We still use sum to record the sum
Sum = 0 # sum = 0 while I <= 3: # set the loop condition sum += I # sum = 1 # I = 1 # ICopy the code
Sum is like a big house, and I is like a porter, carrying numbers from 1 to 3 in and adding them up
4. Then we can change it to the sum of the numbers from 1 to 100 and print the result. The final code:
i = 1 sum = 0 while i <= 100: sum += i i += 1 print(sum)
Copy the code
Title 3:
Print all odd numbers between 1 and 100
i = 1 while i <= 100: if(i%2 ! • print(I) I +=1Copy the code
1. Let’s narrow it down and print numbers 1-5 first, in a loop
i = 1 while i <= 5: print(i) i += 1
Copy the code
The difference between an odd number and an even number is whether it can be integer by 2. In other words, if this number is divided by 2, it is even if the remainder is 0, and odd if the remainder is not 0. We only want odd, so we add a criterion
if(i%2 ! = 0): print(i)Copy the code
3. Then you can print odd numbers 1-5
i = 1 while i <= 5: if(i%2 ! = 0): print(i) i += 1Copy the code
4. Finally, just change the loop condition to within 1-100
i = 1 while i <= 100: if(i%2 ! = 0): print(i) i += 1Copy the code
Topic 4:
Print all even numbers from 1 to 100
I =1 while I <= 100: if(I %2 == 0): • print(I) I +=1Copy the code
So this is a very similar problem to the last one
Title 5:
For 1-2 + 3-4 + 5… The sum of all the numbers of 99
Plan a
sum = 0 i = 1 while i <= 99: if(i%2 == 0): tmp = -i else: tmp = i sum += tmp i+=1 print(sum)
Copy the code
Answer:
1. Let’s narrow down the range and calculate the numbers between 1 and 5. It’s not hard to see from observation that the even numbers here are preceded by a negative sign, so we should find all the even numbers through conditional judgment, and then add a negative sign to them, and then add them to sum
if(i%2 == 0): tmp = -i else: tmp = i
Copy the code
Tip: The temporary TMP variable is used here so as not to affect the loop because I is the key variable in the loop. We only want positive or negative values of I, but if I is negative it affects the loop, so make sure I executes
2. Then you can add sum to sum
if(i%2 == 0): tmp = -i else: tmp = i sum += tmp
Copy the code
3. Finally put the loop on
sum = 0 i = 1 while i <= 5: if(i%2 == 0): tmp = -i else: tmp = i sum += tmp
Copy the code
4. Then change the loop 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)
Copy the code
Scheme 2
Answer:
We can sum them separately, take the odd sum first, and then take the even sum, because the even sum is negative, so the odd sum plus the even sum is the same thing as the odd sum minus the even sum
1. Let’s narrow down the range first, calculate 1-5 and define two variables, which are odd and even and respectively
sum_odd = 0 sum_even = 0
Copy the code
2. We need a condition to determine whether I is odd or even, and if it’s odd we add it to sum_odd, and if it’s even we add it to sum_even
if(i%2 ! = 0): sum_odd += i elif(i%2 == 0): sum_even += iCopy the code
3. Finally loop, then odd and – even sum, 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)Copy the code
4. Finally change the loop condition, the final code:
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)Copy the code