Created
December 28, 2023 15:19
-
-
Save thambaru/1619bb40dc9c5688207f174bc0831f81 to your computer and use it in GitHub Desktop.
Python program that calculates the area of a circle based on the radius entered by the user.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python exercise: https://www.w3resource.com/python-exercises/python-basic-exercises.php | |
# | |
# 4. Write a Python program that calculates the area of a circle based on the radius entered by the user. | |
# | |
# Some online compilers do not allow importing sys. | |
# Remove the following line in such case, and set sysIsAvailable to False. | |
from sys import exit | |
isSysAvailable = True | |
from math import pi | |
r = "" | |
while r.isdigit() is False: | |
if r == "Q" or r == "q": | |
print("exit") | |
if isSysAvailable is True: | |
exit() | |
else: | |
r = "0" | |
continue | |
elif r != "": | |
print("Invalid input. Press Q and hit Enter to exit.") | |
r = input("Please input the radius of the circle:\n") | |
if r != "0": | |
area = pi * (float(r)**2) | |
# The question prefers unformatted number, but here it is in two decimal places: | |
print(format(area, ".2f")) | |
# If you need the preferred answer, comment the above line and uncomment the following line. | |
# print(area) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment