59doit
[Python] Let's python 5장 예제 본문
반응형
1)
다음 height 변수에 별(star)의 층수를 입력하면 각 층 마다 별의 개수가 한 개씩 증가하여 출력되고,
마지막 줄에 별의 개수가 출력되도록 함수의 빈칸을 채우시오.
#<출력결과>
height : 3 <- 키보드 입력햇을때
*
**
***
start 개수 : 6
height = int(input('height : '))
def StarCount(height) :
cnt = 0
for i in range(1,height+1) :
print(i*'*')
cnt = cnt+i
print('start 개수 : %d'%cnt)
StarCount(height)
height :
>? ("3" 입력하면)
*
**
***
start 개수 : 6
2)
중첩함수를 적용하여 <조건>에 맞게 은행계좌 함수의 빈칸을 채우시오.
<조건1> 외부함수 : bank_account() : 잔액(balance) outer변수
<조건2> 내부함수 : getBalance() : 잔액확인 / deposit(money) :입금하기 / withdraw(money) : 출금하기
<조건3> 출금액이 잔액보다 많은 경우 '잔액이 부족합니다.' 메시지 출력
#<출력결과>
최초 계좌의 잔액을 입력하세요 : 1000
현재 계좌 잔액은 1000원 입니다.
입금액을 입력하세요 :
15000원 입금후 잔액은 16000입니다.
출금액을 입력하세요 : 3000
3000원 입금후 잔액은 13000입니다.
bal = int(input('최초 계좌의 잔액을 입력하세요 : '))
def bank_account(bal) :
balance = bal
def getBalance() :
return balance
def depoit(money) :
nonlocal balance
balance = balance + money
print(money, '원 입금 후 잔액은 ', balance, '원 입니다.')
return balance
def withdraw(money) :
nonlocal balance
if balance >= money :
balance= balance - money
print(money, '원 출금 후 잔액은', balance, '원 입니다.')
else :
print('잔액이 부족합니다.')
return getBalance, depoit, withdraw
getBalance, depoit, withdraw = bank_account(bal)
print('현재 계좌 잔액은 :',getBalance(),'원 입니다.')
getBalance()
money=int(input('입금액을 입력하세요 : '))
depoit(money)
money1= int(input('출금액을 입력하세요 :'))
withdraw(money1)
3)
패토리얼 을 계산하는 재귀함수의 빈칸을 채우시오.
def Factorial(n) :
if n==1 :
return 1
else :
result = n * Factorial(n-1)
return result
result_fact = Factorial(5)
print('패토리얼 결과:', result_fact)
# <출력> 2 3 4 5 패토리얼 결과: 120
반응형
'Q.' 카테고리의 다른 글
[Python] Let's python 6장 예제 (0) | 2022.11.03 |
---|---|
[Python]class def 예제 (0) | 2022.11.02 |
[Python] Let's python 4장 예제 (0) | 2022.11.01 |
[Python] 점프투파이썬 03 예제 (0) | 2022.11.01 |
[Python] Let's python 3장 예제 (0) | 2022.10.31 |
Comments