需要有关python编程的帮助

时间:2017-05-29 23:29:22

标签: python-3.x

我正在尝试编写一个计算平均数的程序,并确定用户输入的最大+最小数。我遵循了教练的指导方针,但最后,它搞砸了,我不知道我做错了哪一步。请看一下,给我一些建议,谢谢。

MAX_PINTS = 256    

def valid_real(value):

    try:

        if float(value) >= 0:
            return True
        if float(value) < 0:
            return False

    except ValueError:
            return False



def get_real(prompt):

    value = ""

    value = input(prompt)
    while not valid_real(value):
        print(value, "is not a valid number. Please try again.")
        value = input(prompt)
    return float(value)


def y_or_n(prompt):

    value = ""

    value = input(prompt)
    while True:
        if value == "Yes" or value == "yes":
            return True
        elif value == "No" or value == "no":
            return False
        else:
            print("Please enter yes or no!")
            value = input(prompt)



def get_pints_collected(pints_collected):

    done = False
    counter = 0

    while not done:
        for turn_counter in range (7):


            pints_collected[counter] = get_real("Please enter your collected pints: ")
            counter = counter + 1
        done = y_or_n("Do you want to end program? (Enter yes or no)")

    return counter



def calculate_pints_average(pints_collected):

    total = 0
    minimum = pints_collected[0]
    maximum = pints_collected[0]


    for i in range (7):
        total = total + pints_collected[i]
        if pints_collected[i] > maximum:
            maximum = pints_collected[i]

        if pints_collected[i] < minimum:
            minimum = pints_collected[i]


    pints_average = total / 7




def output(pints_average, maximum, minimum):

    pints_average = 0
    maximum = 0
    minimum = 0

    print ("The average number of pints donated is: ", pints_average)
    print ("The highest pints donated is: ", maximum)
    print ("The lowest pints donated is: ", minimum)


def final():

    pints_collected = [0.0 for x in range (MAX_PINTS) ]
    pints_average = 0.0
    number_pints_collected = 0
    maximum = 0.0
    minimum = 0.0

    pints_collected = get_pints_collected(pints_collected)
    pints_average = calculate_pints_average(pints_collected)
    output(pints_average, maximum, minimum)

final()

0 个答案:

没有答案