Python Forum
[split] problem with function return value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] problem with function return value
#1
HELP ME TO SOLVE THIS problem USING FUNCTION
A teacher in a school wants to find and display the grade of a student based on his/her percentage score. The criterion for grades is as given below:

Score (both inclusive) Grade
Between 80 and 100 A
Between 73 and 79 B
Between 65 and 72 C
Between 0 and 64 D
Any other value Z
Assume that a percentage score is a whole number.
AND I HAVE WRITTEN THIS, BUT ITS NOT RETURNING ANY VALUE
MY CODE IS LIKE THIS
num=0
num=input('enter your score=')
#Start writing your code here
def score(num):
    if num>=80 or num<=100:
        n='A'
    elif num>=73 or num<=79:
        n='B'
    elif num>=65 or num<=72:
        n='c'
    elif num>=0 or num<=64:
        n='d'
    else:
        n='e'
    return n
    num=input('enter your score=')
    print(score)
Reply
#2
There are multitude of problems in your code, for start:
1. remove lines 1 and 2 - you don't need them
2. dedent lines 16 and 17, so that they are not part of the function
3. You need to convert num to int, because input will returns str
4. You need to pass num (after being converted to int) as argument when calling the function on line 17

Note, this will not resolve problems with the logic you implement (i.e. there are more problems in the code)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Use of function/return Paulman 6 2,306 Oct-24-2021, 11:07 PM
Last Post: Paulman
  Multiple return from function Grimmar 7 3,490 Mar-22-2021, 09:20 PM
Last Post: Grimmar
  Lambda function not return value mbilalshafiq 4 3,252 Jul-04-2020, 07:47 AM
Last Post: ndc85430
  Child class function of Log return "None" mbilalshafiq 2 2,180 Jun-30-2020, 07:22 PM
Last Post: mbilalshafiq
  Question on "define function"; difference between return and print extricate 10 4,598 Jun-09-2020, 08:56 PM
Last Post: jefsummers
  Function to return today's month, day, and year sbabu 9 4,828 Jan-28-2020, 06:20 PM
Last Post: snippsat
  return outside function seamus 4 3,021 May-17-2019, 07:38 PM
Last Post: seamus
  Recursive Function - Compare 2 lists, return the elements that don't exist in both KellyBaptist 1 5,181 Dec-23-2018, 10:10 AM
Last Post: Gribouillis
  Need of return in function if statement inside the function already returns Athul 5 3,868 Aug-16-2018, 10:19 AM
Last Post: DuaneJack
  Calling function-- how to call simply return value, not whole process juliabrushett 2 3,168 Jul-01-2018, 01:17 AM
Last Post: juliabrushett

Forum Jump:

User Panel Messages

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