Python Forum
How to make a dynamic variable based on input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make a dynamic variable based on input
#1


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.
Reply
#2
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
Reply
#3
@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
Reply
#4
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to calculate tax with a variable input and if else afefDXCTN 12 22,292 Mar-15-2024, 01:58 AM
Last Post: nave
Question Simulate an answer based on user input [Beginner needs guidance] Bombardini 1 1,256 Nov-12-2022, 03:47 AM
Last Post: deanhystad
  [split] How to calculate tax with a variable input and if else rokemas 1 2,093 Dec-28-2020, 06:53 AM
Last Post: buran
  Changing Directory based on user input paulmerton4pope 13 7,884 Aug-14-2020, 11:48 AM
Last Post: GOTO10
  How to make a variable contain multiple values stevenjwilliams83 10 3,455 Apr-30-2020, 07:22 AM
Last Post: pyzyx3qwerty
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,759 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  New to Python - tiny coding assistance on user input function and assign to variable Mountain_Duck 1 2,464 Mar-23-2019, 06:54 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020