请查看我的代码

时间:2014-03-04 01:58:29

标签: python while-loop

大家好我是编程新手,并试图理解Python中的while循环。

我想打印以下内容

print 2
print 4
print 6
print 8
print 10
print "Goodbye!" 

这是我写的

x = 0
sum = x

while (sum != 10):
    x = x + 2
    print x
    sum = x + 2
    Print ('Good bye!') 

任何人都可以告诉我哪里出错了。

1 个答案:

答案 0 :(得分:1)

最直接的解决方法:

x = 0
sum = x

while (sum <= 10):
    x = x + 2
    print x
    sum = x + 2
print ('Good bye!')  # <-- lower case, unindented

更短的解决方案:

for x in range(2, 12, 2): # start at 2, increment by 2, up to but not including 12
    print x
print 'Good bye!'