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

:::
--------------------------------------------
## 問1:マイナンバーカードの判断
とある国のマイナンバーカードにルールがあり、女性だと偶数で男性だと奇数です。ルールに従って国民のジェンダーを判断ができるプログラムを作成してください。
ただし、毎回判断する人数は定かではないので、入力の回数を自分で決められるようにしましょう。
| 名前 | 番号 |
| -------- | -------- |
| Tom | 208411|
| Samdy | 874540|
| Candy | 438062|
```
**入力例&出力例1:**
The number of people:1
Please input name:Tom
Please input number code:208411
| Name = Tom ,Number = 208411 , Gender = male |
**入力例&出力例2:**
The number of people:3
Please input name:Candy
Please input number code:438062
| Name = Candy ,Number = 438062 , Gender = female |
Please input name:Tom
Please input number code:208411
| Name = Tom ,Number = 208411 , Gender = male |
Please input name:Samdy
Please input number code:874540
| Name = Samdy ,Number = 874540 , Gender = female |
```
:::success
先生の解答は、0が入力されると判定を行わない考えで書きました。
楠楠のは、最低でも1が入力される前提で書いたんじゃないかなと思います。
それでもOKです!
楠楠のコード自体は問題なくプログラムをちゃんと作成できています。コードの考え方としては、例えば:二人「2」が入力された場合、一人分はループの外側で入力するので、ループ内は一回入力すればいいということで、if文でもし「num」が0だったら、input()をしないようにしていると思います。ですので、考え方をもう少し分析してみたら、以下のようにまとめることもできるので参考にしてください!
```python=
num=int(input("The number of people:"))
name=input("Please input name:")
code=int(input("Please input number code:"))
while num >0 :
num-=1
if code%2==0:
print("| Name = {} ,Number = {} , Gender = female |".format(name,code))
else:
print("| Name = {} ,Number = {} , Gender = male |".format(name,code))
if num!=0:
name=input("Please input name:")
code=int(input("Please input number code:"))
```
code:
```python=
num=int(input("The number of people:"))
name=input("Please input name:")
code=int(input("Please input number code:"))
while num != 0:
num-=1
if code%2 == 0 and num==0:
print("| Name = {} ,Number = {} , Gender = female |".format(name,code))
elif code%2 == 0 and num !=0:
print("| Name = {} ,Number = {} , Gender = female |".format(name,code))
name=input("Please input name:")
code=int(input("Please input number code:"))
elif code%2 != 0 and num==0:
print("| Name = {} ,Number = {} , Gender = male |".format(name,code))
elif code%2!=0 and num!=0:
print("| Name = {} ,Number = {} , Gender = male |".format(name,code))
name=input("Please input name:")
code=int(input("Please input number code:"))
```
結果:

:::
## 問2:借金を返そうね!有借有還,再借不難
借金金額と利息の年利率と月々定額の返済額を入力し、
借金がなくなるまで月数と借金金額を表示するプログラムを作ってみましょう。
* 借金は毎月「年利率/12」分増加します。
* 返済した分で減少します。
```
**入力例1:**
Debt:54800
Interest Rate(%):15
Repayment:10000
**出力例1:**
1月、返済額:10000円 借金:45485円
2月、返済額:10000円 借金:36053円
3月、返済額:10000円 借金:26504円
4月、返済額:10000円 借金:16835円
5月、返済額:10000円 借金:7045円
6月、返済額:7134円、完済。
返済総額:57134円
**入力例2:**
Debt:158000
Interest Rate(%):10
Repayment:30000
**出力例2:**
1月、返済額:30000円 借金:129316円
2月、返済額:30000円 借金:100394円
3月、返済額:30000円 借金:71230円
4月、返済額:30000円 借金:41824円
5月、返済額:30000円 借金:12173円
6月、返済額:12274円、完済。
返済総額:162274円
**入力例3:**
Debt:456789
Interest Rate(%):54
Repayment:98765
**出力例3**
1月、返済額:98765円 借金:378579円
2月、返済額:98765円 借金:296850円
3月、返済額:98765円 借金:211443円
4月、返済額:98765円 借金:122193円
5月、返済額:98765円 借金:28927円
6月、返済額:30229円、完済。
返済総額:524054円
```
:::success
* 1-6行目は全く問題ありませんね。
* 7行目も特に問題はなく、「0」に等しくないと条件式立ててもいいです。但し、負の数になった場合も考えないと行けません。ですので、先生だったら最初にif文を加え、借金が返済額以上であるかを判断します。この判断がない場合、借金が返済額より少ないときも同じく引き算行われてしまうので、借金が負の数になってしまいます。
```python=
while debt != 0:
if debt >= repayment:
```
* 次に借金は毎月増加するので、先に増加させます。毎月「年利率/12」分増加するが、入力する際は「%」で入力するので、「年利率/12/100」で%にします。
```python=
debt=debt+debt*(rate/12/100)
#debt+=debt*(rate/12/100)
```
* そして、借金から返済額を引き、monthに1を加算、totalに返済額を加算、出力します。
* では、借金が返済額より少ない時の処理もしましょう。借金を増やし、monthに1を加算、totalに返済額を加算といった処理も必要ですが、返済額を引く処理は必要ありません。何故かというと、例えば借金が3000円しか残ってないとき、10000円を返さなくていいです。つまり、借金が返済額より少ない時、返済額イコール借金ですね。最後に、ループから脱出するため、借金を0にします。
```python=
while debt != 0:
if debt >= repayment:
debt+=debt*(rate/12/100)
debt-=repayment
month+=1
total+=repayment
print("{}月、返済額:{}円 借金:{}円".format(month,repayment,int(debt)))
else:
debt+=debt*(rate/12/100)
repayment=debt
debt=0
month+=1
total+=repayment
```
:::
:::info
code:
```python=
"""
import math
debt=int(input("Debt:"))
rate=int(input("Interest Rate(%):"))
repayment=int(input("Repayment:"))
total=0
month=0
while debt != 0:
debt-=repayment
debt+=math.ceil(rate/12)
month+=1
total+=repayment
print("{}月、返済額:{}円 借金:{}円".format(month,repayment,debt))
print("{}月、返済額:{}円、完済。".format(month,repayment))
print("返済総額:{}円".format(total))
"""
debt = int(input('Debt:'))
interestRate = float(input('Interest Rate(%):'))
repayment = int(input('Repayment:'))
total = 0
month = 0
while True:
if debt > repayment:
total+=repayment
month+=1
debt = debt + debt*(interestRate/12/100) - repayment
print("{}月、返済額:{}円 借金:{}円".format(month,repayment,int(debt)))
elif debt <= repayment:
debt = debt + debt*(interestRate/12/100)
total+=debt
month+=1
break
print("{}月、返済額:{}円、完済。\n返済総額:{}円".format(month,int(debt),int(total)))
```
結果:
:::
## 問3:課金はよくない
736遊戲公司想統計玩家的課金程度,因此聘請一位工讀生來輸入玩家一個月課金的金額,當輸入"end"時停止輸入,並依照以下面的表來統計每個rank有多少人。
736ゲームズカンパニーは、プレイヤーが支払っている金額をカウントしたいと考えています。ですので、バイトの学生を雇い、プレイヤーが1ヶ月間に支払った金額を入力してもらいます。
「end」が入力された時点でストップし、下の表に従って各ランクの人数をカウントして出力するプログラムを作って下さい。
* 実習生が誤って負の数を入力する場合がありますので、無視してください。
| Rank | Money |
| -------- | -------- |
| S | 50,000元以上 |
| A | 10,000元以上 |
| B | 3,000元以上 |
| C | 3,000元未満 |
```
入力例:
Please enter money :80000
Please enter money :30000
Please enter money :8000
Please enter money :5000
Please enter money :1000
Please enter money :500
Please enter money :0
Please enter money :-9000
Please enter money :end
出力例:
Rank S:1
Rank A:1
Rank B:2
Rank C:3
```
:::success
今回はお金を使ったお客様をランク付けするプログラムなので、例えば五万円以上であれば、1を足して一人だということです!。
code:
```python=
"""
money=0
total=0
while True:
money=input("Please enter money :")
if money=="end":
break
else:
total+= int(money)
if money < 3000:
total+=money
print("Rank C:{}".format(total))
elif money < 9999:
total+=money
print("Rank B:{}".format(total))
elif money < 49999:
total+=money
print("Rank A:{}".format(total))
elif money >= 50000:
total+=money
print("Rank S:{}".format(total))
"""
s = 0
a = 0
b = 0
c = 0
while True:
money = input("Please enter money :")
if money == "end":
break
else:
money = int(money)
if money >= 50000:
s+=1
elif money >= 10000:
a+=1
elif money >= 3000:
b+=1
elif money >= 0:
c+=1
else:
continue
print("Rank S:{}".format(s))
print("Rank A:{}".format(a))
print("Rank B:{}".format(b))
print("Rank C:{}".format(c))
```
結果:

:::
## 問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=
tryinput=0
print("「Live or die, make your choice.」")
guess=input("Take a guess :")
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
```
結果:

:::
## スライド練習1
:::info
code:
```python=
"""
NUM=int(input("NUM:"))
inputNum=0
total=0
while NUM != -9999:
total=total+inputNum
if NUM == -9999:
print("sum:{}".format(total))
"""
inputNum=0
total=0
while inputNum != -9999:
total=total+inputNum
inputNum=int(input("num:"))
print("sum:{}".format(total))
```
結果:
:::
## スライド練習2
:::info
code:
```python=
aNum=int(input("Input A:"))
bNum=int(input("Input B:"))
while aNum <= bNum:
print(aNum)
aNum=aNum+1
print()
print("end")
```
結果:
:::
## スライド練習3
:::info
code:
```python=
"""
aNum=int(input("Input A:"))
bNum=int(input("Input B:"))
while aNum<=bNum:
print(aNum)
aNum=aNum+1
if (aNum <= bNum)/5:
print(aNum,end=" ")
aNum+1
print(" ")
print("end")
"""
aNum=int(input("Input A:"))
bNum=int(input("Input B:"))
while aNum<=bNum:
if aNum%5 == 0:
break
print(aNum,end=" ")
aNum+=1
print(" ")
print("end")
```
結果:
:::
## スライド練習4
:::info
code:
```python=
"""
inputNum=0
total=0
while True:
if inputNum != "end":
break
total=total+inputNum
inputNum=int(input("num:"))
print("sum:{}".format(total))
"""
total=0
while True:
num=input()
if num=="end":
break
total+=int(num)
print("sum:{}".format(total))
```
結果:
:::