Programming Languages/Python
[Python Examples] 파이썬 if문 예제: Sphere, Cylinder 계산하기
Henry Cho
2022. 10. 10. 12:02
파이썬 if문 예제: Sphere, Cylinder 계산하기
포스트 난이도: HOO_Intern
# Example Code
val = int(input("Please Enter the Option: 1 is Cylinder and 2 is Sphere: "))
if val == 1:
cr = float(input("Enter a radius: "))
ch = float(input("Enter a height: "))
cylinderCal = 3.14*cr*cr*ch
print("Volume of a Cylinder: ", cylinderCal)
elif val == 2:
sr = float(input("Enter a radius: "))
sphereCal = 4*(3.14*sr*sr*sr/3)
print("Volume of a Shphere: ", sphereCal)
else:
print("Please Enter only 1 or 2.")
Please Enter the Option: 1 is Cylinder and 2 is Sphere: 2
Enter a radius: 5
Volume of a Shphere: 523.3333333333334
Please Enter the Option: 1 is Cylinder and 2 is Sphere: 2
Enter a radius: 101
Volume of a Shphere: 4313526.8533333335
Please Enter the Option: 1 is Cylinder and 2 is Sphere: 3
Please Enter only 1 or 2.
위의 예제 코드는 if와 elif를 사용해서 Sphere와 Cylinder의 Volume을 구하는 코드이다.
if문을 통해서 Sphere를 선택할 것인지Cylinder를 선택할 것인지 정해진다.
그다음에는 Radius에 대한 값을 입력받아 결과를 산출하게 된다.
728x90