36.9k views
0 votes
A certain instructor assigns a 50-point problem and calculates an integer score 0-50 and assigns a letter grade according to the scale 40 - 50 is an A, 30 - 39 is a B, 20 - 29 is a C, 10 - 19 is a D and 0 - 9 is an F. Write a Python function called letter_grade which takes one argument, an integer, and RETURNS the appropriate letter grade.

1 Answer

1 vote

Answer:

Following are the code to this question:

def letter_grade(x): #defining a method letter_grade that accepts one parameter

if x>=50 and x<=40:#defining if block that check value of x greater than 50 and less than 40

print("A")#print value

if x>=30 and x<=39:#defining if block that check value of x greater than 30 and less than 39

print("B")#print value

if x>=20 and x<=29:#defining if block that check value of x greater than 20 and less than 29

print("C")#print value

if x>=10 and x<=19:#defining if block that check value of x greater than 10 and less than 19

print("D")#print value

if x>=0 and x<=9:#defining if block that check value of x greater than 0 and less than 9

print("F")#print value

letter_grade(30) #calling the method letter_grade

Output:

B

Step-by-step explanation:

In the above Python code, a method "letter_grade" is declared, that accepts an integer variable in its parameter, and defined the multiple if block to check the given value, which can be defined as follows:

  • In the first, if block, it checks the value of x greater than 50 and less than 40 , if the condition is true it will print the value "A".
  • In the second, if block, it checks the value of x greater than 30 and less than 39, if the condition is true it will print the value "B".
  • In the third, if block, it checks the value of x greater than 20 and less than 29, if the condition is true it will print the value "C".
  • In the fourth, if block, it checks the value of x greater than 10 and less than 19, if the condition is true it will print the value "D".
  • In the fifth, if block, it checks the value of x greater than 0 and less than 9, if the condition is true it will print the value "F".

And at the last it call the method.

User Robert Sidzinka
by
8.2k points

No related questions found