Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How do i do functions

+6 votes
asked Feb 18, 2022 by Santiago Alvarez Rodriguez (180 points)
GPA = input("What is your GPA: ")
def GPAcalc( A, B, C, D, F):
    (GPA == "A")
    print.str("4")
    (GPA == "B")
    print.str("3")
    (GPA == "C")
    print.str("2")
    (GPA == "D")
    print.str("1")
    (GPA == "F")
    print.str("0")
    (GPA != "A", "B", "C", "D", "F")
    print ("Invalid")
print("Your GPA score is: " + GPA)

what am i doing wrong i dont know how to do this

6 Answers

+1 vote
answered Feb 21, 2022 by chinmay gandhi (200 points)

I don't know what exactly was your question but this python solution might help

https://onlinegdb.com/HbAv4fWFU

The next one is a better approach

https://onlinegdb.com/_-O1-cre3

smiley

+1 vote
answered Feb 21, 2022 by Peter Minarik (86,640 points)

You have to learn the proper syntax:

  • use if to check if a condition is true and branch your code into a true and false case.
  • you can keep using elif to follow the false case of your previous condition and test for a new condition
  • you can add an else if you do not have any other condition to handle the false case.
  • when an expression for a condition is finished, close it with a colon (:)
  • use print to print something on the screen
  • you already seem familiar with input to ask the user to provide some input.
  • you can turn a string (text) into all upper case to make your life easier during the grade checks with the string.upper() function. Since input already returns a string, you can just call upper() on the result of input().

And here's the code:

def CalculateGpa(grade):
    if grade == 'A':
        print(4)
    elif grade == 'B':
        print(3)
    elif grade == 'C':
        print(2)
    elif grade == 'D':
        print(1)
    elif grade == 'E':
        print(0)
    else:
        print('Invalid grade.')

gpa = input("What is your GPA: ").upper()
CalculateGpa(gpa)
0 votes
answered Mar 23, 2022 by Sam (1,310 points)
instead of using a comma you have to have a bunch of or statements or (in terms of my python based answer) it will see it as a tuple not as it checking each one (if you can understand what im saying if you dont just comment)
0 votes
answered Apr 2, 2022 by Nathan Taylor (160 points)
im with you lol
0 votes
answered Apr 2, 2022 by misha356 (220 points)
You have not created a variable with a GPA value. Before the last line of the program, create a variable called GPA, and execute a function in the variable. In a function, in checks, remove the quotes from "A", "B", "C" and "D"
0 votes
answered Apr 7, 2022 by userdotexe (1,340 points)
edited Apr 7, 2022 by userdotexe

I adjusted your definition code, but I'm not totally sure what you want to do with it. But here is the modified code.

GPA = input("What is your GPA: ")

def GPAcalc( A, B, C, D, F):
    if (GPA == "A"):
        print("4")
    elif (GPA == "B"):
        print("3")
    elif (GPA == "C"):
        print("2")
    elif (GPA == "D"):
        print("1")
    elif (GPA == "F"):
        print("0")
    elif (GPA != "A", "B", "C", "D", "F"):
        print ("Invalid")
        
print("Your GPA score is: " + GPA)

Would you mind explaining what you want to do? It would help.

:)

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...