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


Messages In This Thread
RE: How to make a dynamic variable based on input - by loltylerdowney - Nov-30-2017, 09:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to calculate tax with a variable input and if else afefDXCTN 12 22,960 Mar-15-2024, 01:58 AM
Last Post: nave
Question Simulate an answer based on user input [Beginner needs guidance] Bombardini 1 1,290 Nov-12-2022, 03:47 AM
Last Post: deanhystad
  [split] How to calculate tax with a variable input and if else rokemas 1 2,126 Dec-28-2020, 06:53 AM
Last Post: buran
  Changing Directory based on user input paulmerton4pope 13 8,057 Aug-14-2020, 11:48 AM
Last Post: GOTO10
  How to make a variable contain multiple values stevenjwilliams83 10 3,561 Apr-30-2020, 07:22 AM
Last Post: pyzyx3qwerty
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,818 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,519 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