Python Example: Indefinite prompting to determine the corresponding grade for a given mark

Task

Create a program that would repeatedly prompt the user for a mark and outputs the corresponding grade.

Solution using discrete selection statements

confirm= input ("\nWould you like to enter a grade? \n Y for Yes, any other key for no\n")
while (confirm.lower() =="y" ):
    #processdata here
    grade = float(input("please enter a mark\n\n"))
    if (grade<50):
        print("fail")
    elif (grade<65):
        print("D")
    elif (grade<75):
        print("C")
    elif (grade<85):
        print("B")
    else:
        print ("A")
    
    #end dataprocessing
    confirm= input ("\nWould you like to enter a grade? \n Y for Yes, any other key for no\n")
print("end")

Solution using cascading/nested selection statements

confirm= input ("\nWould you like to enter a grade? \n Y for Yes, any other key for no\n")
while (confirm.lower() =="y" ):
    #processdata here
    grade = float(input("please enter a mark\n\n"))
    if (0<=grade and grade<50):
        print("fail")
    if (50<=grade and grade<65):
        print("D")
    if (65<=grade and grade<75):
        print("C")
    if (75<=grade and grade<85):
        print("B")
    if (85<=grade and
        ygrade<=100):
        print ("A")
    
    #end dataprocessing
    confirm= input ("\nWould you like to enter a grade? \n Y for Yes, any other key for no\n")
print("end")

© 2022  Vedesh Kungebeharry. All rights reserved. 

Leave a comment