I havn’t been able to find a solution to this problem on the internet (maybe im not looking hard enough) but i can’t figure out how to make an input only take numbers. Im trying to get the input to go through some equations and the program brakes everytime i put a letter in the input. I was wondering if there was a way to detect if the input was a letter or number. I’ll show my program.
Radius=input("What is the radius of the circle/sphere?")
Areacircle=(int(Radius)**2)*3.14159265359
Perimetercircle=2*3.14159265359*int(Radius)
Permsphere=4*3.14159265359*(int(Radius)**2)
Areasphere=(4/3)*3.14159265359*(int(Radius)**3)
print("The radius' length was:",Radius)
print("The surface area of each circle is:",Areacircle)
print("The perimeter of the circle is:",Perimetercircle)
print("The volume of the sphere would be:",Areasphere)
print("The perimeter of the Sphere would be:",Permsphere)
As suggested in the comments, you can handle a
ValueErrorwhen the conversion tointfails (and save doing this same conversion throughout the rest of your code).I recommend reading the Python Tutorial section on Handling Exceptions, which has a very similar example.