python中包含整数的列表的长度
该程序采用两个正整数(对角线和项)。它的作用是打印出帕斯卡三角形中对角线的数字序列。所以,你输入对角线,直到你想要它返回数字列表的术语为止。我对循环设置了限制,当数字大于100时停止循环。我希望它返回包含小于100的数字的列表的长度python中包含整数的列表的长度,python,Python,该程序采用两个正整数(对角线和项)。它的作用是打印出帕斯卡三角形中对角线的数字序列。所以,你输入对角线,直到你想要它返回数字列表的术语为止。我对循环设置了限制,当数字大于100时停止循环。我希望它返回包含小于100的数字的列表的长度 """ Asks what diagonal and up to what term you want to see the sequence for any diagonal of pascal's triangle """ import math diago
"""
Asks what diagonal and up to what term you want
to see the sequence for any diagonal of pascal's triangle
"""
import math
diagonal = int(input("What diagonal do you want to see?"))
term = int(input("what term do you wan to see?"))
product= term
for i in range (1,term+1):
product = math.factorial(i-1 + diagonal-1)/ (math.factorial(diagonal-1) * math.factorial(i-1))
if product > 100:
break
print product
print(len(str(product)))
它打印出输入的长度,但不打印列表的长度。
(EX)对角线:5;任期:20
它返回的列表:1,5,15,35,70
列表长度:3(应该是5)您没有将结果存储到列表中,
str(product)
的长度显然不正确
product = term
result = [] # here
for i in range (1,term+1):
product = math.factorial(i-1 + diagonal-1)/ (math.factorial(diagonal-1) * math.factorial(i-1))
if product > 100:
break
result.append(product) # here
print(len(result)) # and here