# Python - Fuction(1) - Exercise
###### tags: `Python HomeWork`
**例**:着ぐるみのバイトの求人には幾つの条件があります。特に着ぐるみ自体の大きさが決まっているので身長制限があります。とある会社の求人には、160センチ以下が好ましいと書いていて、もし153センチの杏奈が応募したら果たしてTrueなのかFalseなのか、プログラミングで出力してください。
```
杏奈の身長を表す変数を利用して比較してください
annaHigh = 153
出力例:
True
```
:::info
code:
```python=
annaHigh = 153
print( annaHigh <= 160 )
```
答え:

:::
--------------------------------------------
## 問1:三角形判断
次の条件を満たすプログラムを作成せよ。
1. 3つの辺を入力できるようにし、入力したものを関数judge()に渡します。関数は渡された三辺が1つの三角形として成り立つかどうかを判断し、bool値を返します。
2. もし出来る場合は三角形の周りの長さの値を、出来ない場合は【Invalid】と表示する事。
3. プログラムは「and」や「or」を使う事。
``
補足:三角形の判断方法 → 3つの内いずれ2つの辺の合計が残りの1つの辺より大きくなければならない。
``
### 入力と表示例:
```
**入力例1:**
Input a:4
Input b:5
Input c:6
**出力例1:**
15
**入力例2:**
Input a:1
Input b:2
Input c:6
**出力例2:**
Invalid
```
:::info
Code:
```python=
"""
def add(a,b,c):
judge = a + b + c
return judge
a=int(input("Input a:"))
b=int(input("Input b:"))
c=int(input("Input c:"))
judge = a + b + c
if a+b > c and a+c > b and b+c > a:
print(judge)
elif a+b < c or a+c < b or b+c < a:
print("Invalid")
"""
def judge(a,b,c):
if a+b > c and a+c > b and b+c > a:
return True
else:
return False
a=int(input("Input a:"))
b=int(input("Input b:"))
c=int(input("Input c:"))
if judge(a,b,c):
print(a+b+c)
else:
print("Invalid")
```
結果:

:::
## 問2:総和?昭和?
aからbまでの総和を求める
1. aとbを入力できるようにし、関数swap()に渡します。
2. 関数swap()は渡されたaとbの値を交換し返します。
**forを使用してください**
```
入力例1:
総和を求めます。
a :3
b :8
出力例1:
3から8までの総和は33
入力例2:
総和を求めます。
a :8
b :3
出力例2:
3から8までの総和は33
```
:::info
code:
```python=
"""
def swap(a,b):
swap = a + b
return swap
print("総和を求めます。")
a=int(input("a :"))
b=int(input("b :"))
if a>b:
temp=a
a=b
b=temp
total=0
for i in range(a,b+1):
total+=i
print("{}から{}までの総和は{}".format(a,b,total))
"""
def swap(a,b):
return b,a
def num_sum(fro,to):
total=0
for i in range(a,b+1):
total+=i
return total
print("総和を求めます。")
a=int(input("a :"))
b=int(input("b :"))
if a>b:
a,b=swap(a,b)
total=num_sum(a,b)
print("{}から{}までの総和は{}".format(a,b,total))
```
結果:

:::
## 問3:Christmasツリー
* ユーザーに数値(自然数)を入力させる。
* 入力した数値は関数tree()に渡し、三角形のように「*」を表示させること。
* 関数tree()を二回呼び出すことでツリーにすること。
```
入力例1:
Input n:3
出力例1:
*
***
*****
*
***
*****
入力例2:
Input n:5
出力例2:
*
***
*****
*******
*********
*
***
*****
*******
*********
```
:::info
code:
```python=
"""
def tree(n):
return n
n=int(input("Input n:"))
for i in range(2):
for i in range(n):
for j in range(n-1-i):
print(" ", end = "")
for k in range((i+1)*2-1):
print("*", end = "")
print()
"""
def tree(n):
for i in range(n):
for j in range(n-1-i):
print(" ", end = "")
for k in range((i+1)*2-1):
print("*", end = "")
print()
n=int(input("Input n:"))
tree(n)
tree(n)
```
結果:

:::
## 問4:I want to play a game.
**「Greetings and welcome、I want to play a game.」**

今、あなたは密室に閉じ込められてる。次のプログラムを作成し、制限回数以内に正しい「**animal**」を入力しないと、足枷をはめられ永遠に密室から出ることはないでしょう。
* 入力回数を**3**に制限すること。
* 正しい「**animal**」は「**pig**」である。
**Live or die, make your choice.**
```
入出力例1:
「Live or die, make your choice.」
Take a guess : Lion
I'm still among you. Game have just begun.
Take a guess : Panda
How much blood will you shed to stay alive?
Take a guess : Human
Game over.
--Trapped--
入出力例2:
「Live or die, make your choice.」
Take a guess : pig
You must meet death in order to be reborn. Congratulations
```
:::info
code:
```python=
"""
def add(guess):
guess="pig"
return guess
print("「Live or die, make your choice.」")
guess=input("Take a guess : ")
tryinput=0
while tryinput <= 3:
tryinput+=1
if guess != "pig" and tryinput == 1:
print("I'm still among you. Game have just begun.")
guess=input("Take a guess :")
elif guess != "pig" and tryinput == 2:
print("How much blood will you shed to stay alive?")
guess=input("Take a guess :")
elif guess != "pig" and tryinput == 3:
print("Game over.")
print("--Trapped--")
elif guess == "pig":
print("You must meet death in order to be reborn. Congratulations")
break
"""
def message(count):
if count==0:
s="I'm still among you. Game have just begun."
elif count==1:
s="How much blood will you shed to stay alive?"
elif count==2:
s="Game over.\n--Trapped--"
return s
answer="pig"
print("「Live or die, make your choice.」")
count=0
while count < 3:
guess=input("Take a guess : ")
if guess == "pig":
print("You must meet death in order to be reborn. Congratulations")
break
string=message(count)
print(string)
count+=1
```
結果:

:::
## 問5:貯金は大事
次の条件を満たすプログラムを作成せよ。
ユーザーに金額(例:10,000)、年間利回り(例5.75)、そして経過月数(例5)を入力させ、関数total_deposit()に渡します。関数は、預金総額を計算し返します。
小数点以下2桁まで表示させること。
例えば、 預金額を$10,000とし、年間利回りが5.75%だとする。
1ヶ月目の預金総額は: 10000 + 10000 * 5.75 / 1200 = 10047.92
2か月目の預金総額は: 10047.92 + 10047.92 * 5.75 / 1200 = 10096.06
3か月目の預金総額は: 10096.06 + 10096.06 * 5.75 / 1200 = 10144.44
```
入力例1:
Please input money: 10000
Please input gain: 5.75
Please input month: 5
出力例1:
Your total money: 10241.89
入力例2:
Please input money: 50000
Please input gain: 7.62
Please input month: 24
出力例2:
Your total money: 58203.25
```
:::info
code:
```python=
"""
def total_deposit():
total_deposit=money
return total_deposit
money=int(input("Please input money:"))
gain=float(input("Please input gain:"))
months=int(input("Please input month:"))
for month in range(months):
money=money+money*gain/1200
print("Your total money:{:.2f}".format(money))
"""
def total_deposit(money,gain,months):
for month in range(months):
money=money+money*gain/1200
return money
money=int(input("Please input money:"))
gain=float(input("Please input gain:"))
months=int(input("Please input month:"))
money=total_deposit(money, gain, months)
print("Your total money:{:.2f}".format(money))
```
結果:

:::
## 問6:登録システム
アカウントログイン、作成、切替ができるものを作成してみましょう!
* まず、メニューを以下のフォーマットで表示します。
1. 「-」30個。
2. "Menu","0.Login","1.Create","2.Switch"、
それぞれ表示する際は中央寄せ、フィールド幅28と設定します。
* 次に、どの機能にするかを選択できるようにします
* 選択できるのは以下三つの機能です
1. Login(ログイン):ログインを選択すると、EMAILとPASSWORDを入力し、確認画面を出力します。
3. Create(新規作成):新規作成を選択すると、EMAILとPASSWORDを入力し、NAMEを入力してから、確認画面を出力します。
4. Switch(アカウントの切替):Switchを選択すると、EMAILとPASSWORDを入力し、確認画面を出力します。
**確認画面のフォーマットはMenuと同じです**
**詳しい出力の仕方は、出力例から確認してください**
**関数を使うこと**
```
入出力例1:
------------------------------
| Menu |
------------------------------
| 0.Login |
------------------------------
| 1.Create |
------------------------------
| 2.Switch |
------------------------------
Which one?0
Please Input your E-mail:123@gmail.com
Please Input your Password:123456
------------------------------
| Login |
------------------------------
| 123@gmail.com |
------------------------------
| 123456 |
------------------------------
| Correct? |
------------------------------
入出力例2:
------------------------------
| Menu |
------------------------------
| 0.Login |
------------------------------
| 1.Create |
------------------------------
| 2.Switch |
------------------------------
Which one?1
Please Input your E-mail:123@gmail.com
Please Input your Password:123456
Please Input your name:Banana
------------------------------
| 123@gmail.com |
------------------------------
| 123456 |
------------------------------
| Banana |
------------------------------
| Correct? |
------------------------------
入出力例3:
------------------------------
| Menu |
------------------------------
| 0.Login |
------------------------------
| 1.Create |
------------------------------
| 2.Switch |
------------------------------
Which one?2
Please Input your E-mail:123@gmail.com
Please Input your Password:123456
------------------------------
| Switch Account |
------------------------------
| 123@gmail.com |
------------------------------
| 123456 |
------------------------------
| Correct? |
------------------------------
```
:::info
code:
```python=
"""
select=[0,1,2,3,4]
menu=["Menu","Login","Create","Switch","Correct?"]
print("-"*30)
print("|{:^28}|".format(menu[0]))
print("-"*30)
print("|{:^28}|".format("0."+menu[1]))
print("-"*30)
print("|{:^28}|".format("1."+menu[2]))
print("-"*30)
print("|{:^28}|".format("2."+menu[3]))
print("-"*30)
def choice(user_select):
choice=user_select
return choice
user_select=int(input("Which one?"))
if user_select==0:
email=input("Please Input your E-mail:")
password=int(input("Please Input your Password:"))
print("-"*30)
print("|{:^28}|".format(menu[1]))
print("-"*30)
print("|{:^28}|".format(email))
print("-"*30)
print("|{:^28}|".format(password))
print("-"*30)
print("|{:^28}|".format(menu[4]))
print("-"*30)
elif user_select==1:
email=input("Please Input your E-mail:")
password=int(input("Please Input your Password:"))
name=input("Please Input your name:")
print("-"*30)
print("|{:^28}|".format(email))
print("-"*30)
print("|{:^28}|".format(password))
print("-"*30)
print("|{:^28}|".format(name))
print("-"*30)
print("|{:^28}|".format(menu[4]))
print("-"*30)
elif user_select==2:
email=input("Please Input your E-mail:")
password=int(input("Please Input your Password:"))
print("-"*30)
print("|{:^28}|".format("Switch Account"))
print("-"*30)
print("|{:^28}|".format(email))
print("-"*30)
print("|{:^28}|".format(password))
print("-"*30)
print("|{:^28}|".format(menu[4]))
print("-"*30)
"""
def prtInfo(title,info1,info2,info3):
print("-"*30)
print("|{:^28}|".format(title))
print("-"*30)
print("|{:^28}|".format(info1))
print("-"*30)
print("|{:^28}|".format(info2))
print("-"*30)
print("|{:^28}|".format(info3))
print("-"*30)
def inputInfo():
email=input("Please Input your E-mail:")
password=int(input("Please Input your Password:"))
return email,password
def login():
email,password=inputInfo()
return "Login",email,password,"Correct?"
def create():
email,password=inputInfo()
name=input("Please Input your name:")
return email,password,name,"Correct?"
def switch():
email,password=inputInfo()
return "Switch Account",email,password,"Correct?"
prtInfo("Menu","0.Login","1.Create","2.Switch")
choise=int(input("Which one?"))
if choise==0:
title,info1,info2,info3=login()
elif choise==1:
title,info1,info2,info3=create()
elif choise==2:
title,info1,info2,info3=switch()
prtInfo(title,info1,info2,info3)
```
結果:

:::
## 問7:BODY
アカウントログイン、作成、切替ができるものを作成してみましょう!
* まず、メニューを以下のフォーマットで表示します。
1. 「-」30個。
2. "--Health--","0.BMI","1.BMR","2.Weight"、
それぞれ表示する際は中央寄せ、フィールド幅28と設定します。
* 次に、どの機能にするかを選択できるようにします。
* 選択できるのは以下三つの機能です。
1. BMI:BMI = 体重kg ÷ (身長m)2
1. BMR:ジェンダーによって異なります。
男性: 13.397×体重kg+4.799×身長cm−5.677×年齢+88.362
女性: 9.247×体重kg+3.098×身長cm−4.33×年齢+447.593
3. Weight:適正体重、美容体重、モデル体重を計算します。
適正体重 = (身長m)2 × 22(BMI指数が22で計算された、最も健康的と言われる体重)
美容体重 = (身長m)2 × 20(BMI指数が20で計算された、見た目がスリムな体重)
モデル体重 = (身長m)2 × 18(BMI指数が18で計算された、モデル・女優のような体重)
* すべての機能は計算結果が算出された後、メニューと同じフォーマットで出力します。
**関数を使うこと**
```
入出力例1:
------------------------------
| --Health-- |
------------------------------
| 0.BMI |
------------------------------
| 1.BMR |
------------------------------
| 2.Weight |
------------------------------
本日はどのようなご用件ですか?0
Please Input your cm:168
Please Input your kg:64
------------------------------
| BMI |
------------------------------
| 168 |
------------------------------
| 64 |
------------------------------
| 22.68 |
------------------------------
入出力例2:
------------------------------
| --Health-- |
------------------------------
| 0.BMI |
------------------------------
| 1.BMR |
------------------------------
| 2.Weight |
------------------------------
本日はどのようなご用件ですか?1
Please Input your age:25
Please Input your gender 0:male 1:female =1
Please Input your cm:165
Please Input your kg:55
------------------------------
| BMR |
------------------------------
| 165 |
------------------------------
| 55 |
------------------------------
| 1359.10 |
------------------------------
入出例3:
------------------------------
| --Health-- |
------------------------------
| 0.BMI |
------------------------------
| 1.BMR |
------------------------------
| 2.Weight |
------------------------------
本日はどのようなご用件ですか?2
Please Input your cm:165
適正体重、美容体重、モデル体重は以下の通りです
------------------------------
| Weight |
------------------------------
| 59.89 |
------------------------------
| 54.45 |
------------------------------
| 49.00 |
------------------------------
```
:::info
code:
```python=
"""
import math
select=[0,1,2,3]
menu=["--Health--","BMI","BMR","Weight"]
print("-"*30)
print("|{:^28}|".format(menu[0]))
print("-"*30)
print("|{:^28}|".format("0."+menu[1]))
print("-"*30)
print("|{:^28}|".format("1."+menu[2]))
print("-"*30)
print("|{:^28}|".format("2."+menu[3]))
print("-"*30)
def choice(user_select):
choice=user_select
return choice
user_select=int(input("本日はどのようなご用件ですか?"))
if user_select==0:
cm=int(input("Please Input your cm:"))
kg=int(input("Please Input your kg:"))
BMI=(kg/math.pow((cm/100),2))
print("-"*30)
print("|{:^28}|".format(menu[1]))
print("-"*30)
print("|{:^28}|".format(cm))
print("-"*30)
print("|{:^28}|".format(kg))
print("-"*30)
print("|{:^28.2f}|".format(BMI))
print("-"*30)
elif user_select==1:
age=int(input("Please Input your age:"))
gender=int(input("Please Input your gender 0:male 1:female ="))
cm=int(input("Please Input your cm:"))
kg=int(input("Please Input your kg:"))
if gender==0:
BMR=13.397*kg+4.799*cm-5.677*age+88.362
elif gender==1:
BMR=9.247*kg+3.098*cm-4.33*age+447.593
print("-"*30)
print("|{:^28}|".format(menu[2]))
print("-"*30)
print("|{:^28}|".format(cm))
print("-"*30)
print("|{:^28}|".format(kg))
print("-"*30)
print("|{:^28.2f}|".format(BMR))
print("-"*30)
elif user_select==2:
cm=int(input("Please Input your cm:"))
print("適正体重、美容体重、モデル体重は以下の通りです")
normal_kg=(math.pow((cm/100),2)*22)
beauty_kg=(math.pow((cm/100),2)*20)
model_kg=(math.pow((cm/100),2)*18)
print("-"*30)
print("|{:^28}|".format(menu[3]))
print("-"*30)
print("|{:^28.2f}|".format(normal_kg))
print("-"*30)
print("|{:^28.2f}|".format(beauty_kg))
print("-"*30)
print("|{:^28.2f}|".format(model_kg))
print("-"*30)
"""
import math
def prtInfo(title,info1,info2,info3):
print("-"*30)
print("|{:^28}|".format(title))
print("-"*30)
print("|{:^28}|".format(info1))
print("-"*30)
print("|{:^28}|".format(info2))
print("-"*30)
print("|{:^28}|".format(info3))
print("-"*30)
def inputInfo():
cm=int(input("Please Input your cm:"))
return cm
def bmi():
cm=inputInfo()
kg=int(input("Please Input your kg:"))
BMI=(kg/math.pow((cm/100),2))
return "BMI",cm,kg,"{:^28.2f}".format(BMI)
def bmr():
age=int(input("Please Input your age:"))
gender=int(input("Please Input your gender 0:male 1:female ="))
cm=inputInfo()
kg=int(input("Please Input your kg:"))
if gender==0:
BMR=13.397*kg+4.799*cm-5.677*age+88.362
elif gender==1:
BMR=9.247*kg+3.098*cm-4.33*age+447.593
return "BMR",cm,kg,"{:^28.2f}".format(BMR)
def weight():
cm=inputInfo()
print("適正体重、美容体重、モデル体重は以下の通りです")
normal_kg=(math.pow((cm/100),2)*22)
beauty_kg=(math.pow((cm/100),2)*20)
model_kg=(math.pow((cm/100),2)*18)
return "Weight","{:^28.2f}".format(normal_kg),"{:^28.2f}".format(beauty_kg),"{:^28.2f}".format(model_kg)
prtInfo("--Health--","0.BMI","1.BMR","2.Weight")
choise=int(input("本日はどのようなご用件ですか?"))
if choise==0:
title,info1,info2,info3=bmi()
elif choise==1:
title,info1,info2,info3=bmr()
elif choise==2:
title,info1,info2,info3=weight()
prtInfo(title,info1,info2,info3)
```
結果:

:::
## 問8:カードゲーム
1 ~ 9 の数字が書かれたカードを用いて、簡易的なポーカーで遊びましょう
このゲームでは手札が4枚で1セットです。1セット4枚の組み合わせにより以下の 5 種類の役が存在します。
* **Four Card**:
|9|9|9|9|
|-|-|-|-|
* **Three Card**:
|7|7|7|8|
|-|-|-|-|
* **Two Pair**:
|2|2|5|5|
|-|-|-|-|
* **One Pair**:
|2|2|3|8|
|-|-|-|-|
* **No Pair**:
|1|2|3|4|
|-|-|-|-|
簡易ポーカーの手札 4 枚の数字を小さい順に並べたものが n 個セットを与えられるので、それぞれ上の 5 通りのうちどの役にあたるかを判定するプログラムを作成してください。
:::warning
条件:
* すべてのテストケースにおいて、以下の条件をみたします。
・1 ≦ N ≦ 400
・1 ≦ card_1 ≦ card_2 ≦ s_{i, 3} ≦ s_{i, 4} ≦ 9
・1 ≦ i ≦ N
:::
```
**入力例1**
How many?:8
1セット:7777
2セット:2229
3セット:5566
4セット:2669
5セット:1689
6セット:1333
7セット:1189
8セット:3588
**出力例1**
Four Card
Three Card
Two Pair
One Pair
No Pair
Three Card
One Pair
One Pair
**入力例2**
How many?:2
1セット:1234
2セット:1324
**出力例2**
No Pair
No Pair
```
:::success
テストケースの400は、先生がこのプログラムをテストするとき、400より多いセット数を入力しないことです!
:::
:::info
code:
```python=
"""
n=int(input("How many?:"))
#セットの判断 (うまく機能しません、、、、)(defの中はif文不可能、、、?)
def four_card(input_set):
if input_set == (input_set)*4:
print("Four Card")
def three_card(input_set):
if input_set == (input_set)*3:
print("Three Card")
def two_pair(input_set):
if input_set == ((input_set)*2)*2:
print("Two Pair")
def one_pair(input_set):
if input_set == (input_set)*2:
print("One Pair")
def no_pair(input_set):
if input_set != (input_set)*4 or (input_set)*3 or ((input_set)*2)*2 or (input_set)*2:
print("No Pair")
for i in range(n):
input_set=int(input("{}セット:".format(i+1)))
#条件のところがよくわかりません。(1 ≦ N ≦ 400 の400ってどの範囲、、、?条件2,3のiはどこから出てきた、、、?)
"""
def judge(string):
if string.count(string[0])==2 and string.count(string[2])==2:
return 5
else:
count=0
for n in string:
if string.count(n) > count:
count=string.count(n)
return count
n=int(input("How many?:"))
cardlist=[]
for i in range(n):
card=input("{}セット:".format(i+1))
cardlist.append(card)
result=["No Pair","One Pair","Three Pair","Four Card","Two Card"]
for cards in cardlist:
pair=judge(cards)
print(result[pair-1])
```
結果:

:::