본문 바로가기

Python_Matter/[Check_IO]Elementary

All Upper I

반응형

Quiz>

Check if a given string has all symbols in upper case.

If the string is empty or doesn't have any letter in it - function should return True.

 

Input:

A string.

 

Output:

a boolean.

 

Example:

is_all_upper('ALL UPPER') == True
is_all_upper('all lower') == False
is_all_upper('mixed UPPER and lower') == False
is_all_upper('') == True

 

Precondition:

a-z, A-Z, 1-9 and spaces

 

def is_all_upper(text: str) -> bool:
    # your code here
    return False


if __name__ == '__main__':
    print("Example:")
    print(is_all_upper('ALL UPPER'))

    # These "asserts" are used for self-checking and not for an auto-testing
    assert is_all_upper('ALL UPPER') == True
    assert is_all_upper('all lower') == False
    assert is_all_upper('mixed UPPER and lower') == False
    assert is_all_upper('') == True
    print("Coding complete? Click 'Check' to earn cool rewards!")

 

Solve>

1. isupper()를 사용하여서 입력받은 값이 모두 대문자인지 확인할 수 있다.

    반환값은 모두 대문자인 경우에는 True를 반환한다.

def is_all_upper(text: str):
    if text.isupper() == True:
        return True

 

2. 입력값이 공백이면 True를 반환한다.

   이때 입력값이 스페이스로 공백이 오는 경우가 있는데 그 경우도 True를 반환해야되므로

   strip으로 제거하여서 처리한다.

def is_all_upper(text: str):
    if bool(text.strip()) == False:
        return True

 

3. isdigit()를 사용하여 입력받은 것이 모두 숫자인지 확인할 수 있다.

   반환값은 모두 숫자이면 True를 반환한다.

def is_all_upper(text: str):
    if text.isdigit() == True:
        return True

 

4. 그 이외에 경우는 False를 반환한다.

def is_all_upper(text: str):
    else:
        return False

 

Code>

def is_all_upper(text: str):
    if text.isupper() == True:
        return True

    elif bool(text.strip()) == False:
        return True

    elif text.isdigit() == True:
        return True
        
    else:
        return False

 

Example>

if __name__ == '__main__':
    print("Example:")
    print(is_all_upper('ALL UPPER'))

    # These "asserts" are used for self-checking and not for an auto-testing
    assert is_all_upper('ALL UPPER') == True
    assert is_all_upper('all lower') == False
    assert is_all_upper('mixed UPPER and lower') == False
    assert is_all_upper('') == True
    print("Coding complete? Click 'Check' to earn cool rewards!")

 

Result>

Example:

True

Coding complete? Click 'Check' to earn cool rewards!

반응형

'Python_Matter > [Check_IO]Elementary' 카테고리의 다른 글

Number Length  (0) 2020.04.11
Acceptable Password I  (0) 2020.04.11
Correct Sentence  (0) 2020.04.11
Fizz Buzz  (0) 2020.04.11
Say Hi  (0) 2020.04.11