# grade calculator
# import os
def clear():
# if os.name == "nt":
# os.system("cls")
# else:
# os.system("clear")
#
# module OS is not available in Skulpt, pretend this clears the output
pass
def get():
while True:
try:
one = float(input("How many points did you get?\nPoints scored: "))
clear()
two = float(input("What is the maximum total points?\nMax points: "))
clear()
break
except ValueError:
clear()
input("Error: Please enter a number.\nPress enter to continue...")
clear()
while one > two:
input("Error: Your score cannot be higher than the maximum points.\nPress enter to continue...")
clear()
while True:
try:
one = float(input("How many points did you get?\nPoints scored: "))
two = float(input("What is the maximum total points?\nMax points: "))
break
except ValueError:
input("Error: Please enter a number.\nPress enter to continue...")
clear()
result = one / two * 100
output = "You got a {:.2f}% ({:.2f}/{:.2f})".format(result, one, two)
if result >= 80:
print(output)
elif 70 <= result < 80:
print(output)
else:
print(output)
input("Press enter to continue...")
clear()
# while True:
# save_log = input("Do you want to save the output log? (Y/N): ")
# clear()
# if save_log.upper() == "Y":
# write_mode = "w"
# if os.path.exists("outputs.txt"):
# overwrite = input("Do you want to overwrite (O) the existing file or add (A) to it? (O/A): ")
# clear()
# if overwrite.upper() == "A":
# write_mode = "a"
# with open("outputs.txt", write_mode, encoding="utf-8") as file:
# file.write(output + "\n")
# input("Log saved to outputs.txt\nPress enter to continue...")
# clear()
# break
# elif save_log.upper() == "N":
# break
# else:
# input("Please enter a valid response.\nPress enter to continue...")
#
# Note: File operations don't work in the browser environment.
# The module OS is not available in Skulpt, and file operations like open() will cause errors.
# This functionality would work in a desktop Python environment but is disabled for browser compatibility.
while True:
get()
response = input("Do you want to calculate again? (Y/N): ")
clear()
while response.upper() not in ("Y", "N"):
response = input("Please enter a valid response (Y/N): ")
clear()
if response.upper() == "N":
break
# Note: File operations don't work in the browser environment
# The following code is commented out as it would cause errors when ran in the browser
#
# while True:
# wipe_file = input("Do you want to wipe the contents of outputs.txt? (Y/N): ")
# clear()
# if wipe_file.upper() == "Y":
# with open("outputs.txt", "w"):
# pass
# print("Contents of outputs.txt wiped")
# break
# elif wipe_file.upper() == "N":
# break
# else:
# print("Please enter a valid response (Y/N): ")
input("Press enter to exit...")
clear()
Output
Click "Run Code" to see the program output here
Waiting for input...
How It Works
This program uses a simple formula to calculate your grade percentage:
Grade Percentage
Grade % = (Points Earned / Total Points) × 100
The calculator handles various error cases:
Non-numeric inputs are rejected with an error message
The program allows you to calculate multiple grades in a single session
Results are formatted with two decimal places for precision