Python Forum
Not entirely sure what I did wrong here.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Not entirely sure what I did wrong here.
#1
First of all, I am very new so I apologize if this is a stupid question or if it requires a simple fix.

My goal was to create a function that returns and prints the grade associated with a number given to the code. For example, you put in 65, it prints "The grade for 65 is A" (Ik that's not how it works in schools). So I'm not sure what I did wrong here, if someone could point me in the right direction that would be very helpful.

mrk = 3
grade = returnGrade(mrk)
print("The grade is",grade)

def returnGrade(mrk):
“””return grade
“””
if mark >= 65:
grade = "First"
elif dnum <= 64:
grade = "Upper Second"
elif dnum <= 60:
grade = "Second"
elif dnum <= 50:
grade = "Third"
return grade
else:
grade = "ERROR"
Reply
#2
The biggest issues I see are the indention (which may be a pasting issue), the return statement, the order (can't call a function before definition), and inconsistent variable names. There is a logic error in this too, but you can sort that out.

Based on the structure of your if statement, the return should be at the end only.

def returnGrade(mark):
    if mark >= 65:
        grade = "First"
    elif mark <= 64:
        grade = "Upper Second"
    elif mark <= 60:
        grade = "Second"
    elif mark <= 50:
        grade = "Third"
    else:
        grade = "ERROR"
        
    return grade

mrk = 3
grade = returnGrade(mrk)
print("The grade is", grade)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,565 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  python gives wrong string length and wrong character thienson30 2 3,009 Oct-15-2019, 08:54 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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