查找具有不同三角形的最大路径

时间:2012-04-11 15:19:33

标签: python

我需要找到三角形的最大路径。我之前发布了这个Finding the Maximum Route in a given input我正在实现它,这样每次都可以多次尝试不同的值。我似乎有一个错误..需要一些帮助...我正在使用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

感谢任何帮助。

1 个答案:

答案 0 :(得分:3)

在Python3中,raw_input()(Python2版本)现在只是input()