I am creating a script that calculates the area of a circle from a user input diameter. The input value should be within a defined range.
Is it possible to simplify my script to avoid using multiple while
loops, yet still prompt the user for an input until one is given within the acceptable range, while having custom output "errors" based on if the input is negative or too large? Possibly by using while diameter:
?
This question is different than the linked duplicate because I would like the user to be reprompted for an acceptable input rather than stopping after a single error.
from math import pi
dlist = (range(1,1000))
diameter = int(input("Please enter a diameter between 0 and 1000: "))
while diameter <= 0:
print ("Diameter must be positive")
diameter = (int(input("Please enter a new diameter: ")))
while diameter not in dlist:
print ("Diameter must be less than 1000")
diameter = int(input("Please enter a new diameter: "))
else:
area = (pi*(diameter*0.5)**2)
print ("diameter:", diameter,"area:",area)