Python Forum

Full Version: How to make a dynamic variable based on input
You're currently viewing a stripped down version of our content. View the full version with proper formatting.


Okay so I'm working on this super basic program for my AP Comp Sci class, and I've only just recently started Python so I'm a little lost. I'm making a program in which a student can enter their current grade and then, depending on what grade they would like to end the class with, they are told what grade they need to make on their final exam. Here is my code as it stands:

---------------------------------------------------------------------

cGrade = int(input("What is your current grade in the class?"))
cGrade = cGrade * 4 #to fit the formula to find exam grade needed
A = 450 - int(cGrade)
B = 400 - int(cGrade)
C = 450 - int(cGrade)
D = 300 - int(cGrade)

fGrade = input("What letter grade would you like to end with?")

---------------------------------------------------------------------

(We're working exclusively in PythonRoom right now if that is important)

I want fGrade, the user input, to be entered as a letter grade. So for example if they want their final grade to be A, the program will say something like

if variable(user input = A):
print("In order to end with an A, you will need to score ") + A + (" on your final exam.")

I hope this was clear, as I said I'm super new so I'll take all the tips y'all have to offer.
Hello there, welcome to Python! It would be quite helpful if you used the python BBCode so we can better read your code, which includes all your indentation.

With that being said, this is how I would tackle it.

def grades(): # Define a function, no argument for now so just () will do
    try: # Tells python to try this
        cGrade = int(input("What is your current grade in the class? "))
        cGrade = cGrade * 4 #to fit the formula to find exam grade needed

        a = 450 - cGrade # int(cGrade) is redundant in this case, cGrade is already an interger
        b = 400 - cGrade
        c = 450 - cGrade
        d = 300 - cGrade
        
        # .upper() forces python to read the input as an uppercase letter.
        fGrade = input("What letter grade would you like to end with? ").upper()

        if fGrade == "A": # == means is-equal-to
            print("In order to end with an A, you will need to score",a,"on your final exam.")
        elif fGrade == "B":
            print("In order to end with an B, you will need to score",b,"on your final exam.")
            # To print variables you can use commas print("Word",variable,"Word.")


            # And so on

    except ValueError: # Unless the user doesn't input an integer in this case
        print("Invalid input, try again.")


grades() # Calls are grades() function so it can run
Of course, this is quite basic, you could add more functionality by using loops for example.



BBCode Tutorial

Regards,
Prrz
@Prrz, please don't post homework solutions. I haven't deleted the one you posted since the OP has logged in since you posted it, but too much help isn't better help
(Nov-29-2017, 11:00 PM)Prrz Wrote: [ -> ]Hello there, welcome to Python! It would be quite helpful if you used the python BBCode so we can better read your code, which includes all your indentation.

With that being said, this is how I would tackle it.

def grades(): # Define a function, no argument for now so just () will do
    try: # Tells python to try this
        cGrade = int(input("What is your current grade in the class? "))
        cGrade = cGrade * 4 #to fit the formula to find exam grade needed

        a = 450 - cGrade # int(cGrade) is redundant in this case, cGrade is already an interger
        b = 400 - cGrade
        c = 450 - cGrade
        d = 300 - cGrade
        
        # .upper() forces python to read the input as an uppercase letter.
        fGrade = input("What letter grade would you like to end with? ").upper()

        if fGrade == "A": # == means is-equal-to
            print("In order to end with an A, you will need to score",a,"on your final exam.")
        elif fGrade == "B":
            print("In order to end with an B, you will need to score",b,"on your final exam.")
            # To print variables you can use commas print("Word",variable,"Word.")


            # And so on

    except ValueError: # Unless the user doesn't input an integer in this case
        print("Invalid input, try again.")


grades() # Calls are grades() function so it can run
Of course, this is quite basic, you could add more functionality by using loops for example.



BBCode Tutorial

Regards,
Prrz

Thanks I ended up with two solutions, one that I figured out, and one that a friend of mine showed to me. Turns out a dictionary was the way to go lol