我需要找到三角形的最大路线。我之前在给定的输入中发布了这个寻找最大路线,我正在实现它,以便每次都可以使用不同的值进行多次尝试。我似乎有一个错误..需要一些帮助...我使用的是 Python 3.2.2
代码:
numOfTries = raw_input("Please enter the number of tries")
Tries = int(numOfTries)
for count in range(Tries):
numstr= raw_input("Please enter the height:")
rows = int(numstr)
triangle(rows)
routes(triangle)
max(routes(triangle),key=sum)
def triangle(rows):
for rownum in range (rows):
PrintingList = list()
print ("#%d row") % rownum
for iteration in range (rownum):
newValue = raw_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
错误:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
numOfTries = raw_input("Please enter the number of tries")
NameError: name 'raw_input' is not defined
任何帮助表示赞赏。