简体   繁体   English

三角形数是一个数,它是从 1 到某个整数 n 的整数之和

[英]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.

相关问题 给定一个由 N 个整数组成的数组和一个整数 K,找出该数组中总和等于 K 的元素对的数量 - Given an array of N integers, and an integer K, find the number of pairs of elements in the array whose sum is equal to K 查找具有n个因子的第一个三角数 - Finding the first triangular number with n factors 加权字符串的组合:计算具有sum(integers)= m的整数组合的数量 - Combinatorics of Weighted Strings: Count the number of integer combinations with sum(integers) = m 打印从 1 到 n 的整数(给定数字) - Print the integers from 1 up to n (given number) 从 1 到 n 的整数之和 - Sum of the integers from 1 to n 确定所有正奇数的总和直到某个数字 - determine the sum of all positive odd integers up to some number 编写一个输入正整数n并返回可除以17的n位正整数的数量的函数 - Write a function that input a positive integer n and return the number of n-digit positive integers that divisible by 17 给定一个非负的 integer N,返回类似于 Python 中 N 的非负整数的个数 - Given a non-negative integer N, returns the number of non-negative integers similar to N in Python MyProgrammingLab 三角数问题 - MyProgrammingLab Triangular Number probelm 列总和接近 N 的最小行数,处理非整数 - Minimum number of rows where the column sum is as close to N, dealing with non-integers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM