[英]A triangular number is a number that is the sum of the integers from 1 to some integer n
The full question is A triangular number is a number that is the sum of the integers from 1 to some integer n.完整的问题是三角形数是一个数字,它是从 1 到某个整数 n 的整数之和。 Thus 1 is a triangular number because it's the sum of the integers from 1 to 1;
因此 1 是一个三角形数,因为它是从 1 到 1 的整数之和; 6 is a triangular number because it's 1+2+3=6.
6 是一个三角形数,因为它是 1+2+3=6。 Given the non-negative integer n, create a list of the first n triangular numbers.
给定非负整数 n,创建前 n 个三角形数的列表。 Thus is n was 5, the list would be: [1, 3, 6, 10, 15].
因此 n 为 5,列表将是:[1, 3, 6, 10, 15]。 Associate the list with the variable triangulars.
将列表与变量三角形相关联。
I have to put it in myprogramminglab.我必须把它放在 myprogramminglab 中。
I have tried the following:我尝试了以下方法:
sum=0
triangulars = []
for i in range(1,n+1):
sum += i
triangulars.append(sum)
I am getting an error that triangulars does not contain the correct vale.我收到一个错误,三角形不包含正确的值。
when I put a value in , it says to delete it.当我在 中放入一个值时,它说要删除它。
please help!请帮忙!
Append should be indented within loop and n should be defined Append 应该在循环内缩进,并且应该定义 n
n=int(input('Enter no.of Triangulars you want:'))
triangulars = []
sum=0
for i in range(1,n+1):
sum += i
triangulars.append(sum)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.