Getting started with Python: finding all the numbers of 1-2+3-4+5...99

Source: Internet
Author: User


1 #! / Usr / bin / env python
  2 #-*-coding: utf-8-*-
  3 # Find the sum of all numbers 1-2 + 3-4 + 5 ... 99
  4 "" "
  5 Set start to 1 and sum to 0. The while loop is true when the start is less than 100.
  6 The assignment of temp is equal to the remainder of start and 2. If the assignment of temp is equal to 1, the assignment of sum is equal to
  7 sum plus start assignment (remainder is 1, start is odd), otherwise sum assignment is equal to sum minus start
  8 assignment (the remainder is 0 if the remainder is not 1, and start is even), start is reassigned
  9 is equal to start plus 1, until the assignment of start is equal to 99, the while loop is false! Print sum
10 "" "
11 start = 1
12 sum = 0
13 while start <100:
14 temp = start% 2
15 if temp == 1:
16 sum = sum + start
17 else:
18 sum = sum-start
19 #print (start)
20 #sum = sum + 1
21 start + = 1
22 print (sum) 





Getting started with Python: finding all the numbers of 1-2+3-4+5...99


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.