본문 바로가기

개발/Python

py4e Assignments 3.3

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()

다음도 맞게 동작한다.