-5

y以下语句后的值是多少?

 x = 100
 y = 0
 while x > 50:
     y = y + 1
     x = x - 1

我遇到了涉及 2 个变量的问题。

4

1 回答 1

3

逐步完成循环的前几次迭代,寻找模式并进行推断。

x = 100          # x = 100
y = 0            # x = 100 y = 0
if x > 50:       # x = 100 y = 0
    y = y + 1    # x = 100 y = 1
    x = x - 1    # x = 99  y = 1
if x > 50:       # x = 99  y = 1
    y = y + 1    # x = 99  y = 2
    x = x - 1    # x = 98  y = 2
if x > 50:       # x = 98  y = 2
    y = y + 1    # x = 98  y = 3
    x = x - 1    # x = 97  y = 3
if x > 50:       # x = 97  y = 3
    y = y + 1    # x = 97  y = 4
    x = x - 1    # x = 96  y = 4
if x > 50:       # x = 96  y = 4
    y = y + 1    # x = 96  y = 5
    x = x - 1    # x = 95  y = 5
if x > 50:       # x = 95  y = 5
    y = y + 1    # x = 95  y = 6
    x = x - 1    # x = 94  y = 6
...
if x > 50:       # x = 52  y = 48
    y = y + 1    # x = 52  y = 49
    x = x - 1    # x = 51  y = 49
if x > 50:       # x = 51  y = 49
    y = y + 1    # x = 51  y = 50
    x = x - 1    # x = 50  y = 50
if x > 50:       # x = 50  y = 50
(false, end process)
于 2013-09-23T02:26:13.803 回答