3.3 Write a program to prompt for a score between 0.0 and 1.0. If the score is out of range, print an error. If the score is between 0.0 and 1.0, print a grade using the following table:
Score Grade
>= 0.9 A
>= 0.8 B
>= 0.7 C
>= 0.6 D
< 0.6 F
If the user enters a value out of range, print a suitable error message and exit. For the test, enter a score of 0.85.
try와 except을 배워 적용하는 문제다.
배운 대로 해봤으나 안됨.... 좌절하며 스택오버플로우를 뒤졌더니 딱 맞는 해결책은 없었다.
raw = input("Enter Score: ")
score = float(raw)
try:
if score>=0.0 and score<=1.0:
if score >= 0.9 and score <=1.0:
print("A")
elif score >= 0.8 and score <0.9:
print("B")
elif score >= 0.7 and score <0.8:
print("C")
elif score >= 0.6 and score <0.7:
print("D")
elif score < 0.6:
print("E")
else:
print("out of range")
except:
"Please enter a score number between 0.0 and 1.0"
quit()
내 해답.
raw = input("Enter Score: ")
score = float(raw)
try:
if score >= 0.9 and score <=1.0:
print("A")
elif score >= 0.8 and score <0.9:
print("B")
elif score >= 0.7 and score <0.8:
print("C")
elif score >= 0.6 and score <0.7:
print("D")
elif score < 0.6:
print("E")
except:
"Please enter a score number between 0.0 and 1.0"
quit()
다음도 맞게 동작한다.
'개발 > Python' 카테고리의 다른 글
파이썬 무료 강의 추천 : 모두를 위한 파이썬 Python for everybody (0) | 2020.11.28 |
---|---|
py4e 8.5 From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008 (0) | 2020.11.28 |
py4e exercise 7.2 (0) | 2020.11.28 |
class, function, object, instance, parameter, variable (0) | 2020.08.26 |
Pycharm pyqt5 관련 오류 (타임퍼센트 AI 주식투자 강의) (0) | 2020.08.22 |