gpt4 book ai didi

python - 迭代从 0 开始

转载 作者:太空宇宙 更新时间:2023-11-04 06:45:54 26 4
gpt4 key购买 nike

当我运行下面的代码时,迭代从 0 开始。我想将行更改为从第 1 行开始。我该如何更改它?我尝试在迭代开始前输入 rownum=1。

代码:

def triangle(rows):
for rownum in range (rows):
PrintingList = list()
print ("Row no. %i" % rownum)
for iteration in range (rownum):
newValue = input("Please enter the %d number:" %iteration)
PrintingList.append(int(newValue))
print()
def routes(rows,current_row=0,start=0):
for i,num in enumerate(rows[current_row]): #gets the index and number of each number in the row
if abs(i-start) > 1: # Checks if it is within 1 number radius, if not it skips this one. Use if not (0 <= (i-start) < 2) to check in pyramid
continue
if current_row == len(rows) - 1: # We are iterating through the last row so simply yield the number as it has no children
yield [num]
else:
for child in routes(rows,current_row+1,i): #This is not the last row so get all children of this number and yield them
yield [num] + child

numOfTries = input("Please enter the number of tries:")
Tries = int(numOfTries)
for count in range(Tries):
numstr= input("Please enter the height:")
rows = int(numstr)
triangle(rows)
routes(triangle)
max(routes(triangle),key=sum)

输出:

Please enter the number of tries:2
Please enter the height:4
Row no. 0
Row no. 1
Please enter the 0 number:

感谢任何帮助。

最佳答案

改变:

for rownum in range (rows):

到:

for rownum in range (1, rows+1):

关于python - 迭代从 0 开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10140250/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com