接收用户输入并测试它是否是浮点数

时间:2017-07-28 16:29:32

标签: python

我正在尝试创建一个函数,它将从列表中接收输入并验证它是否为浮点值,如果是,则继续执行该程序,如果不是,请让用户输入第二次回答。新值应该在与前一个不正确的值相同的索引处进入列表。

例如,如果有人在我的代码中输入了值'那么'而不是72,我希望inputHandler函数接收这个不正确的值,告诉用户它是无效的,并要求用户再次回答相同的问题。

我希望我的函数使用try-except-else语句。这是我的代码:

QUIZ_GRADES = int(input("How many quiz grades? "))

PROGRAM_GRADES = int(input("How many program grades? "))

TESTS = int(input("How many tests? "))

def main():

    globalConstantList = [QUIZ_GRADES, PROGRAM_GRADES, TESTS]

    scoreList = [] 

    returnedScoreList = getGrades(globalConstantList,scoreList)

    returnedScoreValue = inputHandler(returnedScoreList)

    returnedScoreValue2 = inputHandler(returnedScoreList)

    returnedScoreListSum, returnedScoreListLength = totalList(returnedScoreList)

    returnedScoreListAverage = calcAverage(returnedScoreListSum,
                                       returnedScoreListLength)

    returnedLetterGrade = determineGrade(returnedScoreListAverage)

    userOutput(returnedScoreListAverage,returnedLetterGrade)

def getGrades(globalConstantList,scoreList):

    for eachScore in globalConstantList:

    #totalScoreList = 0.0

    index = 0

    for index in range(QUIZ_GRADES):

        print("What is the score for", index + 1)

        scoreList.append(float(input()))

        index += 1

    for index in range(PROGRAM_GRADES):

        print("What is the score for", index + 1)

        scoreList.append(float(input()))

        index += 1

    for index in range(TESTS):

        print("What is the score for", index + 1)

        scoreList.append(float(input()))

        index += 1

    return scoreList

def inputHandler(scoreList):

    index = 0

    try:

        print("What is the score for", index + 1)

        scoreList.append(float(input()))

        return scoreList

    except ValueError:

        print("Your value is not correct. Try again.")

        print("What is the score for", index + 1)

        scoreList.append(float(input()))

        return scoreList


def totalList(newScoreList):

    returnedScoreListLength = len(newScoreList)

    returnedScoreListSum = sum(newScoreList)

    return returnedScoreListSum, returnedScoreListLength

def calcAverage(newScoreListSum, newScoreListLength):

    returnedScoreListAverage = newScoreListSum / newScoreListLength

    return returnedScoreListAverage

def determineGrade(newScoreListAverage):

    if newScoreListAverage >= 90:

       return 'A'

    elif newScoreListAverage >= 80:

       return 'B'

    elif newScoreListAverage >= 70:

       return 'C'

    elif newScoreListAverage >= 60:

       return 'D'

    else:

      return 'F'

def userOutput(newScoreListAverage, newLetterGrade):

    print("Your overall grade is",format(newScoreListAverage,'.2f'))

    print("Your letter grade is",newLetterGrade)

    print()
main()

2 个答案:

答案 0 :(得分:0)

据我了解,您想检查用户的输入并将其转换为float。如果成功,你想继续,如果没有,你想再问一次。

假设是这种情况,您可能想要编写一个要求用户输入的函数,尝试将输入转换为float,如果成功则返回它。

def input_float(prompt):
    while True:
        try:
            inp = float(input(prompt))
            return inp
        except ValueError:
            print('Invalid input. Try again.')

f = input_float('Enter a float')
print(f)

然后,您可以使用此代码段作为进一步处理用户提供的f(浮点数)的起点。

答案 1 :(得分:-2)

您可以使用if ifif语句检查您的数字float或int或string,然后在代码正文中执行您的工作

num =  input("Enter a number ") 
if type(num ) == int : print "This number is an int" 
elif type(num ) == float : print "This number is a float"

这里你可以使用函数来反复调用这段代码,并将这段代码放在该函数中,并使用try bock来捕获异常。