Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.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_Python 3.x_Insertion Sort - Fatal编程技术网

Python 插入排序:打印时不给我任何

Python 插入排序:打印时不给我任何,python,python-3.x,insertion-sort,Python,Python 3.x,Insertion Sort,我搜索了整个堆栈,但仍然无法使其工作。我正在学习如何构造算法,并且需要编写一个algroithm(使用插入排序)来按升序排序数组并显示它包含的值。目前我得到了无 def sortedRainfall (rainfall): month = ['January','Febuary','March','April','May','June','July','August'\ ,'September','October','November',

我搜索了整个堆栈,但仍然无法使其工作。我正在学习如何构造算法,并且需要编写一个algroithm(使用插入排序)来按升序排序数组并显示它包含的值。目前我得到了

 def sortedRainfall (rainfall):
        month = ['January','Febuary','March','April','May','June','July','August'\
                    ,'September','October','November','December']
        for index in range(1,len(rainfall)):
            value = rainfall[index]
            i = index - 1
            while (i >= 0):
                if value < rainfall[i]:
                    rainfall[i+1] = rainfall[i] #shift number in slot i right  to slot i + 1
                    rainfall[i] = value #shif value left into slot i
                    i = i - 1
                else:
                    break

        return
def-sortedRainfall(降雨):
月份=[‘一月’、‘二月’、‘三月’、‘四月’、‘五月’、‘六月’、‘七月’、‘八月’\
“九月”、“十月”、“十一月”、“十二月”]
对于范围(1,len(降雨量))内的指数:
数值=降雨量[指数]
i=指数-1
而(i>=0):
如果数值<降雨量[i]:
降雨量[i+1]=降雨量[i]#将插槽i中的编号向右移动到插槽i+1
降雨量[i]=值#将值移到插槽i中
i=i-1
其他:
打破
返回
完整代码如下:

#Main module --------------------------------------------------------------------------------------------------------------
def main ():
    rainfall = rainInput () #Gets rainfall per month from user.
    totalRain = totalRainfall (rainfall) #Totals rainfall from user inputted data.
    average_Rainfall = averageRainfall (totalRain) #Averages rainfall for the year.
    highestMonth, highestMonthly = highestMonthNumber (rainfall) #Finds highest month of rainfall and displays to the user. 
    lowestMonth, lowestMonthly = lowestMonthNumber (rainfall) #Finds lowest month of rainfall and displays to the user. ---
    rainfallSorted = sortedRainfall(rainfall)
    print #this is for spacing output
    print ('The total rainfall for the year was: ' +str(totalRain) + ' inche(s)')
    print #this is for spacing output
    print ('The average rainfall for the year was: ' +str(average_Rainfall) +\
          ' inche(s)') 
    print #this is for spacing in output
    print ('The highest amount of rain was', highestMonthly, 'in' , highestMonth)
    print #this is for spacing in output
    print ('The lowest amount of rain was', lowestMonthly, 'in' , lowestMonth)
    print(rainfallSorted)


#Gets rainfall per month from user. ---------------------------------------------------------------------------------------
def rainInput ():
    rainfall = ['January','Febuary','March','April','May','June','July','August'\
    ,'September','October','November','December']
    month = 0

    while month < len(rainfall):
        rainfall[month] = int(input ('Please enter the amount for month ' + str\
        (month + 1) + ': '))
        month += 1
        rainfall.append

    return rainfall

#Totals rainfall from user inputted data. ---------------------------------------------------------------------------------
def totalRainfall (rainfall):
    return sum([int(x) for x in rainfall])

#Averages rainfall for the year. ------------------------------------------------------------------------------------------
def averageRainfall (totalRain):
    average_Rainfall = totalRain / 12

    return average_Rainfall

#Finds highest month of rainfall and displays to the user. ----------------------------------------------------------------
def highestMonthNumber (rainfall):
    month = ['January','Febuary','March','April','May','June','July','August'\
                ,'September','October','November','December']
    highestMonthly = 0
    highestMonth = 0
    for m, n in enumerate(rainfall):
        if n > highestMonthly:
            highestMonthly = n
            highestMonth = m

    return month[highestMonth], highestMonthly

#Finds lowest month of rainfall and displays to the user. -----------------------------------------------------------------
def lowestMonthNumber (rainfall):
    month = ['January','Febuary','March','April','May','June','July','August'\
                ,'September','October','November','December']
    lowestMonthly = 1000000000
    lowestMonth = 0
    for m, n in enumerate(rainfall):
        if n < lowestMonthly:
            lowestMonthly = n
            lowestMonth = m

    return month[lowestMonth], lowestMonthly

def sortedRainfall (rainfall):
    month = ['January','Febuary','March','April','May','June','July','August'\
                ,'September','October','November','December']
    for index in range(1,len(rainfall)):
        value = rainfall[index]
        i = index - 1
        while (i >= 0):
            if value < rainfall[i]:
                rainfall[i+1] = rainfall[i] #shift number in slot i right  to slot i + 1
                rainfall[i] = value #shif value left into slot i
                i = i - 1
            else:
                break

    return

main()
#主模块--------------------------------------------------------------------------------------------------------------
defmain():
Rainsion=rainInput()#从用户处获取每月降雨量。
totalRain=总降雨量(降雨量)#用户输入数据的总降雨量。
平均降雨量=平均降雨量(总降雨量)#该年的平均降雨量。
highestMonth,highestMonthNumber=highestMonthNumber(降雨量)#查找降雨量最高的月份并向用户显示。
lowestMonth,lowestMonthly=lowestMonthNumber(降雨量)#查找降雨量最低的月份并显示给用户---
rainfallSorted=SortedDrainFall(降雨)
打印#用于间距输出
打印('当年总降雨量为:'+str(总降雨量)+'英寸)
打印#用于间距输出
打印('当年的平均降雨量为:'+str(平均降雨量)+\
"寸(s)
打印#用于输出中的间距
打印('最高降雨量为',最高月降雨量,'年',最高月降雨量)
打印#用于输出中的间距
打印('最低降雨量为',最低月份,'英寸',最低月份)
打印(Rainfall排序)
#从用户处获取每月降雨量---------------------------------------------------------------------------------------
def rainInput():
降雨量=[‘一月’、‘二月’、‘三月’、‘四月’、‘五月’、‘六月’、‘七月’、‘八月’\
“九月”、“十月”、“十一月”、“十二月”]
月份=0
月份最高每月:
最高每月=n
最高月份=m
返回月份[最高月份],最高月份
#查找降雨量最低的月份并显示给用户-----------------------------------------------------------------
def最低月数(降雨量):
月份=[‘一月’、‘二月’、‘三月’、‘四月’、‘五月’、‘六月’、‘七月’、‘八月’\
“九月”、“十月”、“十一月”、“十二月”]
最低每月=100000000
最低月数=0
对于枚举中的m,n(降雨量):
如果n<每月最低值:
最低月份=n
最低月数=m
返回月[lowestMonth],lowestMonthly
排水沟(降雨):
月份=[‘一月’、‘二月’、‘三月’、‘四月’、‘五月’、‘六月’、‘七月’、‘八月’\
“九月”、“十月”、“十一月”、“十二月”]
对于范围(1,len(降雨量))内的指数:
数值=降雨量[指数]
i=指数-1
而(i>=0):
如果数值<降雨量[i]:
降雨量[i+1]=降雨量[i]#将插槽i中的编号向右移动到插槽i+1
降雨量[i]=值#将值移到插槽i中
i=i-1
其他:
打破
返回
main()
def sortNames(名称列表):
sortedNames=[]
对于范围(1,len(listOfNames))中的索引:
值=名称列表[索引]
i=指数-1
而(i>=0):
如果值<名称列表[i]:
listOfNames[i+1]=listOfNames[i]#将插槽i中的编号向右移动到插槽i+1
listOfNames[i]=值#将值左移到插槽i中
i=i-1
其他:
打破
回归雨量
只是需要归还一些东西

试试这个:

def sortedRainfall (rainfall):

for i in range(1, len(rainfall)):
    if rainfall[i] < rainfall[i-1]:
        for j in range(i):
            if rainfall[i] < rainfall[j]:
                rainfall[i], rainfall[j] = rainfall[j], rainfall[i]
return rainfall
def-sortedRainfall(降雨):
对于范围(1,len(降雨量))内的i:
如果降雨量[i]<降雨量[i-1]:
对于范围(i)中的j:
如果降雨量[i]<降雨量[j]:
降雨量[i],降雨量[j]=降雨量[j],降雨量[i]
回归雨量

因为在sortedRainfall函数中没有返回任何内容。。那么,您是否可以使用
排序
功能?不。我希望我可以:/(Psst,如果您找到了解决方案,您应该将其标记为解决方案)。
def sortedRainfall (rainfall):

for i in range(1, len(rainfall)):
    if rainfall[i] < rainfall[i-1]:
        for j in range(i):
            if rainfall[i] < rainfall[j]:
                rainfall[i], rainfall[j] = rainfall[j], rainfall[i]
return rainfall