Thread: Python 3 HELP

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Python 3 HELP


    Hey guys, I am new to the thread(clearly), and I am new to programming altogether. I can't seem to figure out how to make this program and I have tried and tried different ways along with many Google/YouTube searches before coming here.

    The objective of this assignment is to demonstrate your understanding of functions and a menu controlled interface by using them in your solution to this programming assignment.

    Instructions:

    Create a program with three functions and call each of the functions within a while loop.

    The first function takes no parameters, nor does it return a value. It simply displays a menu of selections.

    The second function takes one parameter and returns a value. The parameter is a numeric grade, e.g. 88. The value returned is the associated letter grade as determined by the University grade scale.

    The third function takes two parameters and computes a value based on the values of its parameters and returns the result. The first parameter is the numeric grade and the second is the associated letter grade. The value returned is the number of extra credit points, in 1/2 point increments, needed to raise the letter grade one small step, # e.g. from C to C+.

    The program should remember and display the numeric grade entered by the user immediately after the menu is displayed. The menu should be displayed for each pass through the while loop.


    This is what I have so far:

    #
    import random
    import time

    def get_letter_grade(percentage):
    if percentage >= 95:
    letter_grade = "A"
    elif percentage >= 92:
    letter_grade = "A-"
    elif percentage >= 89:
    letter_grade = "B+"
    elif percentage >= 86:
    letter_grade = "B"
    elif percentage >= 83:
    letter_grade = "B-"
    elif percentage >= 80:
    letter_grade = "C+"
    elif percentage >= 77:
    letter_grade = "C"
    elif percentage >= 74:
    letter_grade = "C-"
    elif percentage >= 71:
    letter_grade = "D+"
    elif percentage >= 68:
    letter_grade = "D"
    elif percentage >= 65:
    letter_grade = "D-"
    else:
    percentage <= 65
    letter_grade = "F"

    def menu_choice():
    print('1: Enter grade in percentage (0-100)')
    print('2: Get letter grade from percentage grade')
    print('3: Calculate letter grade with extra credit')
    print('4: Calculate exctra credit needed to boost letter grade')
    print('0: Exit')
    print()
    return input ("Please make your choice:")

    loop = 1
    choice = 0
    while loop == 1:
    choice = menu_choice()
    if choice == 1:
    print("Enter percentage grade(0-100)?")
    elif choice ==2:
    print(letter_grade)
    elif choice ==3:
    print(percentage + (input))
    elif choice ==4:
    print()
    elif choice ==0:
    loop = 0


    #####
    output I'm looking for:

    1: Enter grade in percentage (0-100)
    2: Get letter grade from percentage grade
    3: Calculate letter grade with extra credit
    4: Calculate extra credit needed to boost letter grade
    0: Exit
    Current grade: 0

    Please make a choice:1

    Enter percentage grade(0-100)? 75

    1: Enter grade in percentage (0-100)
    2: Get letter grade from percentage grade
    3: Calculate letter grade with extra credit
    4: Calculate extra credit needed to boost letter grade
    0: Exit
    Current grade: 75.0

    Please make a choice: 2

    Your letter grade is: C-

    1: Enter grade in percentage (0-100)
    2: Get letter grade from percentage grade
    3: Calculate letter grade with extra credit
    4: Calculate extra credit needed to boost letter grade
    0: Exit
    Current grade: 75.0

    Please make a choice: 3

    Enter extra credit points: 3
    If you can earn 3.0 points extra credit your letter grade would be: C

    1: Enter grade in percentage (0-100)
    2: Get letter grade from percentage grade
    3: Calculate letter grade with extra credit
    4: Calculate extra credit needed to boost letter grade
    0: Exit
    Current grade: 75.0

    Please make a choice: 4

    In order to boost your grade from C- to C, 2.0 points extra credit are needed.

    1: Enter grade in percentage (0-100)
    2: Get letter grade from percentage grade
    3: Calculate letter grade with extra credit
    4: Calculate extra credit needed to boost letter grade
    0: Exit
    Current grade: 75.0

    Please make a choice: 0

    Done!
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Norcross, GA (again)
    Posts
    1,805
    Rep Power
    1570
    For future reference, you should know that the forum software does not by default retain indentation. In order to show the code in a readable and meaningful manner, you need to use either [code] or [highlight] tags around the code sample. There are buttons at the top of the editing window that you can use to do this automatically, as well - the hash (#) button is for plain code samples, the highlight marker button for larger code samples. Since you are new here, I have done this for you to show you the difference:

    Python Code:
     
    #
    import random
    import time
     
    def get_letter_grade(percentage):
        if percentage >= 95:
            letter_grade = "A"
        elif percentage >= 92:
            letter_grade = "A-"
        elif percentage >= 89:
            letter_grade = "B+"
        elif percentage >= 86:
            letter_grade = "B"
        elif percentage >= 83:
            letter_grade = "B-"
        elif percentage >= 80:
            letter_grade = "C+"
        elif percentage >= 77:
            letter_grade = "C"
        elif percentage >= 74:
            letter_grade = "C-"
        elif percentage >= 71:
            letter_grade = "D+"
        elif percentage >= 68:
            letter_grade = "D"
        elif percentage >= 65:
            letter_grade = "D-"
        else:
            percentage <= 65
            letter_grade = "F"
     
    def menu_choice():
        print('1: Enter grade in percentage (0-100)')
        print('2: Get letter grade from percentage grade')
        print('3: Calculate letter grade with extra credit')
        print('4: Calculate extra credit needed to boost letter grade')
        print('0: Exit')
        print()
        return input ("Please make your choice:")
     
    loop = 1
    choice = 0
    percentage = 0
    while loop == 1:
        choice = menu_choice()
        if choice == 1:
            print("Enter percentage grade(0-100)?")
        elif choice ==2:
            print(letter_grade)
        elif choice ==3:
            print(percentage + (input))
        elif choice ==4:
            print()
        elif choice ==0:
            loop = 0

    That having been said, can you tell us where you are having trouble with it?

    I can see at least one major problem right off the bat - in the return value for menu_choice(), you are returning a string, not an integer. You can convert the input value to an integer, but that could raise an exception; it may be easier just to have choice as a string, and compare it to strings:
    Code:
    loop = 1
    choice = ''
    letter_grade = ''
    percentage = 0
    while loop == 1:
        choice = menu_choice()
        if choice == '1':
            print("Enter percentage grade(0-100)?")
        elif choice =='2':
    Note that I also added initializers for percentage and letter_grade, to make sure that they are visible outside of the loop.

    Next, you never read in a value for percentage under option 1. You need to use the input() function to get the value you want; but that isn't quite enough by itself, as you'll also need to convert the result to an integer, and catch any user mistakes that crop up. That means you need what is called an exception frame around your int() cast:
    Code:
        if choice == '1':
            try:
                percentage = int(input("Enter percentage grade(0-100)?"))
            except:
                print("Please try again.")
    Also, you are treating the variable letter_grade as if it were a global, but as it is written here it is actually two different variables in different scopes. What you probably want to do is return the value of letter_grade at the end of get_letter_grade(), like so:
    Code:
        return letter_grade
    Then, you need to call get_letter_grade() in your main program, and assign the local variable letter_grade the returned value:
    Code:
        elif choice =='2':
            letter_grade = get_letter_grade(percentage)    
            print(letter_grade)
    Anything else?

    Oh, I might add that there are more Pythonic ways to write get_letter_grade(), ones which would simplify writing the fourth function as well. I would recommend using a dictionary object, if you have been shown how to use those yet.

    Comments on this post

    • zSnipeAce agrees
    Last edited by Schol-R-LEA; November 1st, 2012 at 07:43 PM.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

IMN logo majestic logo threadwatch logo seochat tools logo