Python four days -- while exercise, python four days -- while
--- Restore content start ---
Exercise questions
1. Calculate the value of 1-2 + 3-4 + 5-6 + 7-8 + 9-10.
Problem Analysis: first write out the even and odd numbers from 1 to 10
#! /Usr/bin/evn python #-*-coding: UTF-8 # evaluate the value of 1-2 + 3-4 + 5-6 + 7-8 + 9-10 # first obtain the even number and odd number I = 0 while True between 1 and 10: I = I + 1 if I> 10: break m = I % 2 if m = 1: print (I) elif m! = 1: print (I)
The preceding questions can be regarded as the sum of all odd numbers minus the sum of all even numbers (1 + 2 + 3 + 5 + 7 + 9)-(2 + 4 + 6 + 8 + 10)
#! /Usr/bin/evn python #-*-coding: UTF-8 # evaluate the value of 1-2 + 3-4 + 5-6 + 7-8 + 9-10 # first calculate the even number between 1 and 10 I = 0x = 0y = 0 while True: I = I + 1 if I> 10: break m = I % 2 if m = 1: x = x + I else: y = y + iprint (x) print (y) print (x)-(y)
The value of x is the sum of all odd numbers.
--- Restore content end ---