파이썬 다중 if문 예

2019. 5. 25. 18:31파이썬

728x90

<문제>

 

시험점수를 입력받아 다음과 같이 학점을 출력하세요.

 

점수가 100 이하, 90 이상이면 A

점수가 89 이하, 80 이상이면 B

점수가 79 이하, 70 이상이면 C

 

시험점수 입력: 85

85점 B입니다.

 

 

 

<옳은 예1>

 

score=int(input("시험점수 입력: "))

if score >= 70 and score <= 79 :
  print("{} 점 C입니다.".format(score))
elif score >= 80 and score <= 89 :
  print("{}점 B입니다.".format(score))
elif score >= 90 and score <= 100 :
  print("{}점 A입니다.".format(score))

 

 

 

<옳은 예2>

score=int(input("시험점수 입력: "))

if score >= 90 :
  print("{} 점 A입니다.".format(score))
elif score >= 80 :
  print("{}점 B입니다.".format(score))
elif score >= 70 :
  print("{}점 C입니다.".format(score))

 

 

문제에서 요구하는 결과값만 구하려면 위 내용만큼만 코딩해도 된다.

 

 

입력되는 점수 범위를 0~100으로만 한정하고

70점 미만에게 F를 주고

0점은 퇴학메시지를 주고

그 외의 숫자를 입력하면 프로그램이 종료되도록 하고 싶다면, 아래 <옳은 예3>을 참고하도록 하자.

 

 

<옳은 예3>

 

score=int(input("시험점수 입력: ")) 

if score <70 and score >0 :
  print("{}점 F입니다.".format(score))
elif score >= 70 and score <= 79 : 
  print("{} 점 C입니다.".format(score)) 
elif score >= 80 and score <= 89 : 
  print("{}점 B입니다.".format(score)) 
elif score >= 90 and score <= 100 : 
  print("{}점 A입니다.".format(score))
elif score == 0 :
  print("{}점 퇴학입니다.".format(score))
else :
  print("오류! 프로그램 종료.")

728x90
반응형