파이썬, 모든 프로그램의 처리 동작
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)))
#함수 시간에 확인했듯 함수 호출 전에 선언, 정의되어야 한다!
간단한 함수와 사용자 입력으로 계산기? 를 만들어 보았어요~
'Programming > Python' 카테고리의 다른 글
#Python - 18. 파이썬 for in list (0) | 2020.01.17 |
---|---|
#Python - 16. 파이썬 리스트 사용 (0) | 2020.01.16 |
#Python - 14. 파이썬 정수와 실수 (0) | 2020.01.16 |
#Python - 13. 파이썬 문자열 (0) | 2020.01.16 |
#Python - 12. 파이썬 format (0) | 2020.01.15 |
댓글