• 生活就是这样,需要去灌溉!
    • 谢谢您的关注,欢迎您的注册与写作!
    • 循环往复,生生不息,或许这就是生命的意义吧!生命是插曲的产品吧!
    • 古今多少事,都付笑言中!
    • 风住尘香花已尽,日晚倦梳头。物是人非事事休,欲语泪先流。
    • 闻说双溪春尚好,也拟泛轻舟,只恐双溪舴艋舟,载不动许多愁。

Python基础知识一:while循环之2-3+4-5…+100

Python 柳叶扉鸿 4年前 (2021-03-14) 1589次浏览 已收录 扫描二维码
内容目录
# 写代码 - while 循环
# 使用 while 循环实现输出 2 - 3 + 4 - 5 + 6 ... + 100 的和,即 2-100 的偶数加,奇数减

"""
#   审题问题和思路不对
# total = 0
# num = 1
# while num < 101:
#     if num % 2 == 0:
#         total += num
#         print("01:", total)
#     else:
#         total -= num
#         print("02:", total)
#     num += 1
# print("03:", total)

#   审题问题和思路不对
total = 0
num = 1
while num < 101:
    if num % 2 == 0:
        # total += num
        # print("01:", total)
        print("01:您的分数为:{}".format(total + num))
    else:
        # total -= num
        print("02:您的分数为:{}".format(total - num))
    num += 1
print("03:", total)

#   审题清晰和思路打开,format 不对
total = 0
num = 2
while num < 101:
    if num % 2 == 0:
        # total += num
        # print("01:", total)
        print("01:您的分数为:{}".format(total + num))
    else:
        # total -= num
        print("02:您的分数为:{}".format(total - num))
    num += 1
print("03:", total)
"""

#   最终完成
total = 0
num = 2

while num < 101:
    if num % 2 == 0:  # 2   偶数加,奇数减
        total += num  # 0+2-3+4
        print("01:", total)
        # print("01:您的分数为:{}".format(total + num))
    else:
        total -= num  # 0+2-3+4-5
        print("02:", total)
        # print("02:您的分数为:{}".format(total - num))
    num += 1
print("03:", total)  # 0+2-3+4-5+6-7+8-9...

柳叶扉鸿 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Python基础知识一:while循环之2-3+4-5…+100
相关文章 相关文章 相关文章
喜欢 (3)