根据Python 3.7中的字符串输入调用特定函数

时间:2019-02-12 16:23:15

标签: python

我正在尝试基于用户的用户输入来调用函数。例如,如果用户在计算器中输入“ P”,则将70点添加到“点”变量中;如果用户在计算器中输入“ M”,则将80点添加到“点”变量中等各种输入都为“ P”,“ M”和“ D”时,将调用一个功能,显示用户已实现的总积分。我尝试使用到目前为止所知的内容在下面的代码片段中自己执行此操作,可惜我遇到了一些错误。我探索了解决此问题的方法,但是发现的大部分内容对我来说还是不够清楚,或者不是我要找的答案。

    def Start():
    user_answer = input("Welcome to the UCAS point converter! Would you like to calculate your UCAS points and grade?: ")
    if (user_answer == "yes" or user_answer == "yes".upper()):
        Collect_Grades()
    else:
        Start()

def Collect_Grades():
    unit1 = input("Please input the grade for the 1st unit: ")
    unit2 = input("Please input the grade for the 2nd unit: ")
    unit3 = input("Please input the grade for the 3rd unit: ")
    unit4 = input("Please input the grade for the 4th unit: ")
    unit5 = input("Please input the grade for the 5th unit: ")
    unit6 = input("Please input the grade for the 6th unit: ")
    unit7 = input("Please input the grade for the 7th unit: ")
    unit8 = input("Please input the grade for the 8th unit: ")
    unit9 = input("Please input the grade for the 9th unit: ")
    unit10 = input("Please input the grade for the 10th unit: ")
    unit11 = input("Please input the grade for the 11th unit: ")
    unit12 = input("Please input the grade for the 12th unit: ")
    unit13 = input("Please input the grade for the 13th unit: ")
    unit14 = input("Please input the grade for the 14th unit: ")
    unit15 = input("Please input the grade for the 15th unit: ")
    unit16 = input("Please input the grade for the 16th unit: ")
    unit17 = input("Please input the grade for the 17th unit: ")
    unit18 = input("Please input the grade for the 18th unit: ")
    print("Thankyou for entering your grades.")
    Calculate_BTEC(unit1,unit2,unit3,unit4,unit5,unit6,unit7,unit8,unit9,unit10,unit11,unit12,unit13,unit14,unit15,unit16,unit17,unit18)

def Calculate_BTEC(unit1,unit2,unit3,unit4,unit5,unit6,unit7,unit8,unit9,unit10,unit11,unit12,unit13,unit14,unit15,unit16,unit17,unit18):
    unit1_bool = False
    unit2_bool = False
    unit3_bool = False
    unit4_bool = False
    unit5_bool = False
    unit6_bool = False
    unit7_bool = False
    unit8_bool = False
    unit9_bool = False
    unit10_bool = False
    unit11_bool = False
    unit12_bool = False
    unit13_bool = False
    unit14_bool = False
    unit15_bool = False
    unit16_bool = False
    unit17_bool = False
    unit18_bool = False
    Points = 0
    if (unit1 == "P"):
        Points = Points + 70
        unit1_bool = unit1_bool = True
    elif (unit2 == "P"):
        Points = Points + 70
        unit2_bool = unit2_bool = True
    elif (unit3 == "P"):
        Points = Points + 70
        unit3_bool = unit3_bool = True
    elif (unit4 == "P"):
        Points = Points + 70
        unit4_bool = unit4_bool = True
    elif (unit5 == "P"):
        Points = Points + 70
        unit5_bool = unit5_bool = True
    elif (unit6 == "P"):
        Points = Points + 70
        unit6_bool = unit6_bool = True
    elif (unit7 == "P"):
        Points = Points + 70
        unit7_bool = unit7_bool = True
    elif (unit8 == "P"):
        Points = Points + 70
        unit8_bool = unit8_bool = True
    elif (unit9 == "P"):
        Points = Points + 70
        unit9_bool = unit9_bool = True
    elif (unit10 == "P"):
        Points = Points + 70
        unit10_bool = unit10_bool = True
    elif (unit11 == "P"):
        Points = Points + 70
        unit11_bool = unit11_bool = True
    elif (unit12 == "P"):
        Points = Points + 70
        unit12_bool = unit12_bool = True
    elif (unit13 == "P"):
        Points = Points + 70
        unit13_bool = unit13_bool = True
    elif (unit14 == "P"):
        Points = Points + 70
        unit14_bool = unit14_bool = True
    elif (unit15 == "P"):
        Points = Points + 70
        unit15_bool = unit15_bool = True
    elif (unit16 == "P"):
        Points = Points + 70
        unit16_bool = unit16_bool = True
    elif (unit17 == "P"):
        Points = Points + 70
        unit17_bool = unit17_bool = True
    elif (unit18 == "P"):
        Points = Points + 70
        unit18_bool = unit18_bool = True
    elif (unit1_bool == True and unit2_bool == True and unit3_bool == True and unit4_bool == True and unit5_bool == True and unit6_bool == True and unit7_bool == True and unit8_bool == True and unit9_bool == True and unit10_bool == True and unit11_bool == True and unit12_bool == True and unit13_bool == True and unit14_bool == True and unit15_bool == True and unit16_bool == True and unit17_bool == True and unit18_bool == True):
        Results(Points)
        

def Fail():
    print("Sorry but you do not have enough grades for an extended diploma")
    exit()

def Results(Points):
    print(str(Points))
    final_answer = input("Would you like to repeat the process?: ")
    if (final_answer == "yes"):
        Start()
    else:
        exit()
    
Start()    

1 个答案:

答案 0 :(得分:2)

这里是使用循环以及上面推荐的listdict数据结构来保持代码干燥的解决方案的快速尝试(“不要重复自己”)。它还使用简单的函数来分解代码并使代码更具可读性,并通过根据dict对其进行检查来对输入进行验证。

请注意,由于您没有在原始示例中包含如何确定通过/失败的信息,因此我假设它基于1000个总分的任意阈值。您可以将其更改为实际的方法/阈值。

# dict to store the number of points each grade is worth
GRADE_POINTS = {
    "F": 0,
    "P": 70,
    "M": 80,
    "D": 90,
}

# enter the actual pass threshold, or change the print_results function below
PASS_THRESHOLD = 1000


def get_grade(unit):
    # loop forever until the user enters a valid grade
    while True:
        grade = input("Please enter the grade from Unit {}: ".format(unit)).upper()
        if grade in GRADE_POINTS:
            return grade


def get_unit_grades(num_units):
    unit_grades = []

    for unit in range(1, num_units + 1):
        unit_grades.append(get_grade(unit))

    return unit_grades


def calculate_BTEC_points(unit_grades):
    point_total = 0

    for grade in unit_grades:
        point_total += GRADE_POINTS[grade]

    return point_total


# change this function to the actual method of calculating results
def print_results(point_total):
    print("You have a total of {} points.".format(point_total))

    if point_total > PASS_THRESHOLD:
        print("Congratulations! You have enough grades for an extended diploma.")
    else:
        print("Sorry, but you do not have enough grades for an extended diploma.")


def main():
    print("Welcome to the UCAS point converter!")
    if input("Would you like to calculate your UCAS points and grade?: ").lower() != "yes":
        return

    # loop forever until the user no longer wants to repeat the process
    while True:
        unit_grades = get_unit_grades(num_units=18)
        point_total = calculate_BTEC_points(unit_grades)

        print_results(point_total)

        if input("Would you like to repeat the process?: ").lower() != "yes":
            return


if __name__ == "__main__":
    main()
相关问题