python while循环求1-1/3+1/5
时间: 2023-10-01 14:10:36 浏览: 131
使用while循环来计算1-1/3+1/5-1/7+...的结果可以通过以下Python程序实现:
```python
sum1 = 0
i = 1
while i <= 100:
sum1 += (-1) ** (i + 1) / (2 * i - 1)
i += 1
print(sum1)
```
在这个程序中,使用while循环来迭代变量i,从1到100。在每次循环中,计算当前项的值,使用(-1) ** (i + 1)来实现正负交替的效果,并将结果加到sum1中。最后,打印出sum1的值,即为所求的结果。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [Python第三次作业_编程实现:s=1/2-2/3+3/4-4/5+…+98/99-99/100_星号金字塔_数字金字塔_空心三角_空心菱形...](https://blog.csdn.net/XQC_KKK/article/details/121193027)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文