编写以下python编码以生成1到55之间的三角形数字。但编码不起作用的原因是什么?
num = 1
sum = 0
while (num <= 10)
sum = sum + num
num = num + 1
print (sum, end=' ')
答案 0 :(得分:2)
缺少冒号:
num = 1
sum = 0
while (num <= 10):
sum = sum + num
num = num + 1
print (sum, end=' ')
或
num = 1
sum = 0
while (num <= 10):
sum = sum + num
num = num + 1
print (sum, end=' ')
输出
1 3 6 10 15 21 28 36 45 55
2.7
num = 1
sum = 0
while (num <= 10):
sum = sum + num
num = num + 1
print sum,
答案 1 :(得分:1)
您的错误是一个错误,后面没有while语句,后跟()
换行和打印结束,Python 2.x中默认不支持。
更正后的代码为:
num = 1
sum = 0
while (num <= 10):
sum = sum + num
num = num + 1
print sum