码迷,mamicode.com
首页 > 编程语言 > 详细

python006(求1-2+3-4+5.....99的所有数的和)

时间:2020-05-10 11:03:41      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:start   python   code   str   pytho   lse   ==   style   div   

# 求1-2+3-4+5.....99的所有数的和(含等式)
s = ""

sum = 0
start = 1

while start < 100:
temp = start % 2
if temp == 1:
if start == 1:
s = str(start)
else:
s = s + "+" + str(start)
sum = sum + start
else:
s = s + "-" + str(start)
sum = sum - start
start += 1

print(s)
print(start)

 

# 求1-2+3-4+5.....99的所有数的和(不含等式)
sum = 0
start = 1

while start < 100:
temp = start % 2
if temp == 1:
sum = sum + start
else:
sum = sum - start
start += 1

print(sum)
print(start)

 1 # 求1-2+3-4+5.....99的所有数的和(含等式)
 2 s = ""
 3 
 4 sum = 0
 5 start = 1
 6 
 7 while start < 100:
 8     temp = start % 2
 9     if temp == 1:
10         if start == 1:
11             s = str(start)
12         else:
13             s = s + "+" + str(start)
14         sum = sum + start
15     else:
16         s = s + "-" + str(start)
17         sum = sum - start
18     start += 1
19     
20 print(s)
21 print(start)
22 
23 
24 
25 # 求1-2+3-4+5.....99的所有数的和(不含等式)
26 sum = 0
27 start = 1
28 
29 while start < 100:
30     temp = start % 2
31     if temp == 1:
32         sum = sum + start
33     else:
34         sum = sum - start
35     start += 1
36     
37 print(sum)
38 print(start)

 

python006(求1-2+3-4+5.....99的所有数的和)

标签:start   python   code   str   pytho   lse   ==   style   div   

原文地址:https://www.cnblogs.com/bigdata-6/p/12862316.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!