Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中将三角形作为输入_Python - Fatal编程技术网

在Python中将三角形作为输入

在Python中将三角形作为输入,python,Python,我试图创建一个三角形,它接受用户输入的值,并从这些值中找到这些输入的最大路径。我最初提出这个问题是为了找到这条路的最大路线: 代码: def triangle(rows): for rownum in range (rows): PrintingList = list() print ("Row no. %i" % rownum) for iteration in range (rownum): newValue =

我试图创建一个三角形,它接受用户输入的值,并从这些值中找到这些输入的最大路径。我最初提出这个问题是为了找到这条路的最大路线:

代码:

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)
def三角形(行):
对于范围内的rownum(行):
PrintingList=list()
打印(“行号%i”%rownum)
对于范围内的迭代(rownum):
newValue=输入(“请输入%d编号:”%iteration)
PrintingList.append(int(newValue))
打印()
def路由(行,当前行=0,开始=0):
对于i,枚举中的num(行[当前_行]):
#获取行中每个数字的索引和编号
如果abs(i-start)>1:#检查它是否在1个数字半径范围内,如果不在1个数字半径范围内,则跳过这一个。如果不使用(0您正在使用:

routes(triangle)
三角形
名称引用一个函数,它作为第一个参数
传递给函数
路由
。在函数体中,
行[当前行]
产生错误,因为
确实是一个函数


我真的不明白你想做什么。也许你想从
三角形
返回
打印列表
,然后将结果依次传递给函数
路由

正如回溯所述,它在25和第10行中。这是说函数不可订阅,这是真的,你不能订阅函数ion。但是,您可以订阅:

String:  "x"[2] == "foo"
Tuple:   (2,5,2,7)[3] == 7
List:    [1,2,3,"foo"][3] == "foo"
Dict:    {"a":1, "b":5, "c":5}["a"] == 1

您可能正在尝试获取
打印列表
的值,该值在
三角形
函数中创建,作为
路由
函数中的
变量

为了让程序以这种方式工作,您必须在三角形函数中添加一个return语句-也就是说,添加一个
return PrintingList
作为最后一个语句-并在调用函数时存储该值,并将存储的值传递给
ROUTS
函数-这意味着程序的结尾应该是我喜欢:

result = triangle(rows)
routes(result)
max(routes(triangle),key=sum)
这将解决此问题,上面的代码中可能还有其他问题。

什么是
路由(三角形)
应该是什么意思?三角形(行)不返回任何内容。
result = triangle(rows)
routes(result)
max(routes(triangle),key=sum)