2012-04-11 62 views
0

我需要查找三角形的最大路线。我早些时候发布了这个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 

任何帮助表示赞赏。

回答

3

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