Skip to content

Instantly share code, notes, and snippets.

@thambaru
Created December 28, 2023 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thambaru/1619bb40dc9c5688207f174bc0831f81 to your computer and use it in GitHub Desktop.
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.
# 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