Class 2 Lab 3 ‐ Advanced Conditions - Justin-Boyd/Python-Class GitHub Wiki
Step 1: Request the user to input a grade and assign it to a variable named grade.
grade = input("Enter a grade: ")
Step 2: Convert the variable grade into an integer.
- Note: Add the command int() to the code line from step 1.
grade = int(input("Enter your grade: "))
Step 3: Create a condition that checks if the grade is equal to or higher than 90 and if so, print A.
if grade >= 90:
print("A")
Step 4: Continue the condition to check if the grade is equal to or higher than 80, and if so, print B.
elif grade >= 80:
print("B")
Step 5: Continue the condition to check if the grade is equal to or higher than 70, and if so, print C.
elif grade >= 70:
print("C")
Step 6: Continue the condition to check if the grade is equal to or higher than 65, and if so, print D.
elif grade >= 65:
print("D")
Step 7: Continue the condition to check if the grade is equal to or higher than 0, and if so, print F.
elif grade >= 0:
print("F")
Step 8: Finally, the code should print an error message if the user inserted an invalid number, such as a negative number.
else:
print("ERROR: Grades cannot be negative numbers or words.")
Final Code
# explains purpose of the script
print("This program will calculate and display the letter grade of a numerical value\n")
grade = float(input("Enter a grade: "))
if grade >= 90:
print("Letter grade: A")
elif grade >= 80:
print("Letter grade: B")
elif grade >= 70:
print("Letter grade: C")
elif grade >= 65:
print("Letter grade: D")
elif grade >= 0:
print("Letter grade: F")
# Will be printed if the value is a negative number or a word.
else:
print("ERROR: Grades cannot be negative numbers or words.")
# prevents program from closing upon execution
input("\nPress 'Enter' to exit the program")