Python中如何求1-100的奇数和?可以用while循环实现,在循环内部变量n不断自减,直到变为【-1】时,不再满足while条件,循环推出,代码为【for i in range(0,100):if i%2==1:sum += i;】。Python求1到100的奇数和的方法:只要条件满足,就不断循环,条件不满足时退出循环。sum = 0 n = 99 while n...
1-100内偶数的和 >>> sum = 0 >>> for i in range(101): if i % 2 == 0: sum += i >>> print(sum) 2550 1. 2. 3. 4. 5. 6. 7. 8. 2、while语句 方法1: >>> sum = 0 >>> i = 100 >>> while i > 0: sum += i i = i - 1 >>> print(sum) 5050 1. 2. 3...