简体   繁体   中英

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. Thus 1 is a triangular number because it's the sum of the integers from 1 to 1; 6 is a triangular number because it's 1+2+3=6. Given the non-negative integer n, create a list of the first n triangular numbers. Thus is n was 5, the list would be: [1, 3, 6, 10, 15]. Associate the list with the variable triangulars.

I have to put it in 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

n=int(input('Enter no.of Triangulars you want:'))

triangulars = []

sum=0

for i in range(1,n+1):

        sum += i

        triangulars.append(sum)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM