test test
test test

Reputation: 21

Call a specific function based on the input of a string in Python 3.7

I am trying to call a function based on user input from the user. For example, if the user inputs "P" into the calculator, then 70 points would be added to the "Points" variable and if the user inputs "M" into the calculator, then 80 points would be added to the "Points" variable, etc. And when all of the various inputs are "P" and "M" and "D" then a function will be called displaying the total amount of points that the user has achieved. I tried doing this myself in the code snippet below using what I know so far, sadly I ran into some errors. I explored the solutions to this issue, but the majority of what I found either was not clear enough to me or was not the answer I was looking for.

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

Upvotes: 2

Views: 47

Answers (1)

Phydeaux
Phydeaux

Reputation: 2855

Here is a quick stab at a solution that uses loops and the list and dict data structures recommended above to keep the code DRY ("Don't Repeat Yourself"). It also uses simple functions to break up the code and make it more readable, and performs validation on the input by checking it against the dict.

Note that since you didn't include in your original example how the pass/fail is determined, I have assumed it is based on an arbitrary threshold of 1000 total points. You can change this to the actual method/threshold.

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

Upvotes: 2

Related Questions