python - 插入排序: Giving me none when I print

标签 python python-3.x insertion-sort

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

 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 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()

最佳答案

def sortNames(listOfNames):
    sortedNames = []
    for index in range(1,len(listOfNames)):
        value = listOfNames[index]
        i = index - 1
        while (i >= 0):
            if value < listOfNames[i]:
                listOfNames[i+1] = listOfNames[i] #shift number in slot i right  to slot i + 1
                listOfNames[i] = value #shif value left into slot i
                i = i - 1
            else:
                break
    return rainfall

只是需要退回一些东西。

关于python - 插入排序: Giving me none when I print,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43750588/

相关文章:

python - 视频捕获后整个帧旋转

c++ - 如何在插入排序中使用 replace() 使语句变得不必要

c# - 在 C# 中对字符串数组进行插入排序

python - 从数据库或 session 数据填充字段的正确方法是什么?

Python - 仅使用循环删除空格

python - 如何将字典列表转换为元组列表?

c++ - 如何从文件中读取结构数组并对其进行排序?

python - IF 语句导致 webpy 出现内部服务器错误

python - Maya Python 更新属性

python - 无法到达类参数中的函数