#Python - 15. 파이썬 사용자 입력 받기
본문 바로가기
Programming/Python

#Python - 15. 파이썬 사용자 입력 받기

by 권가 2020. 1. 16.

파이썬, 모든 프로그램의 처리 동작

def sum(no):
    return no + 10
def sub(no):
    return no - 10
def mul(no):
    return no * 10
def div(no):
    return no / 10
print('수를 하나 선택해주세요.')
mine = float(input())
result_sum = sum(mine)
print('선택된 수:', mine)
print('{}+10={}'.format(mine, result_sum))
print('{}+10={}'.format(mine, sub(mine)))
print('{}+10={}'.format(mine, mul(mine)))
print('{}+10={}'.format(mine, div(mine)))

#함수 시간에 확인했듯 함수 호출 전에 선언, 정의되어야 한다!

 

하지만! 이렇게 하면 사용자 입력 부분 뒤 개행문자가 붙어 다음 라인에서 입력을 받게 됩니다!

그리하여 아래의 코드와 같이

1. print('수를 하나 선택해주세요.', end=' ') -> end=' ' 명령어를 붙여 끝을 개행이 아닌 ' '한 칸의 공백으로 바꿔줄 수 있습니다.

2. input('수를 하나 선택해주세요.') -> input 파이썬 내장 메서드에는 print()메서드가 내장되어 있어 출력과 입력을 동시에 해줄 수 있습니다. 

def sum(no):
    return no + 10
def sub(no):
    return no - 10
def mul(no):
    return no * 10
def div(no):
    return no / 10

mine = float(input('수를 하나 선택해주세요.'))
#print('수를 하나 선택해주세요.', end=' ')
#mine = float(input())
result_sum = sum(mine)
print('선택된 수:', mine)
print('{}+10={}'.format(mine, result_sum))
print('{}+10={}'.format(mine, sub(mine)))
print('{}+10={}'.format(mine, mul(mine)))
print('{}+10={}'.format(mine, div(mine)))

#함수 시간에 확인했듯 함수 호출 전에 선언, 정의되어야 한다!

간단한 함수와 사용자 입력으로 계산기? 를 만들어 보았어요~

댓글