# Python - for(2) -Exercise ###### tags: `Python HomeWork` :::info 楠楠!!!!!!!不會的地方用コメントで考え方教えて~~~~ ::: **例**:着ぐるみのバイトの求人には幾つの条件があります。特に着ぐるみ自体の大きさが決まっているので身長制限があります。とある会社の求人には、160センチ以下が好ましいと書いていて、もし153センチの杏奈が応募したら果たしてTrueなのかFalseなのか、プログラミングで出力してください。 ``` 杏奈の身長を表す変数を利用して比較してください annaHigh = 153 出力例: Ture ``` :::info code: ```python= annaHigh = 153 print( annaHigh <= 160 ) ``` 答え: ![](https://i.imgur.com/SM2qRLJ.png) ::: -------------------------------------------- ## 問一:総和?昭和?(whileでお願いシャス)^^ aからbまでの総和を求める ``` 入力例1: aからbまでの総和を求めます。 a :3 b :8 出力例1: 3から8までの総和は33 入力例2: aからbまでの総和を求めます。 a :8 b :3 出力例2: 3から8までの総和は33 ``` :::success 楠!楠! whileって条件が成立している間に繰り返すループですよね! 10行目と11行目の変数はそれぞれ1を足していたら、total変数の値は永遠とb変数に追いつかないので、無限ループになってしまいます。もうちょっと数値の変化に意識してみてー!!!!! code: ```python= print("aからbまでの総和を求めます。") a=int(input("a :")) b=int(input("b :")) if a>b: temp=a a=b b=temp total=0 while (total > b): b+=1 total+=1 print(total,end=" ") print("{}から{}までの総和は{}".format(a,b,total)) print("aからbまでの総和を求めます。") a=int(input("a :")) b=int(input("b :")) if a>b: temp=a a=b b=temp newA=a total=0 while (a<=b): total+=a a+=1 print("{}から{}までの総和は{}".format(newA,b,total)) ``` 結果: ![](https://i.imgur.com/ZJFoEsO.png) ::: ## 問二:貯金は大事(whileでお願いシャス)^^ 次の条件を満たすプログラムを作成せよ。 ユーザーに金額(例:10,000)、年間利回り(例5.75)、そして経過月数(例5)を入力させ、毎月の預金総額を計算する事。 四捨五入し、小数点以下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 出力例2: 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= money=int(input("Please input money:")) gain=float(input("Please input gain:")) months=int(input("Please input month:")) while months!=months: money=money+money*gain/1200 if months==months: money=money+money*gain/1200 print("Your total money:{:.2f}".format(money)) money=int(input("Please input money:")) gain=float(input("Please input gain:")) months=int(input("Please input month:")) count=1 while count<=months: money=money+money*gain/1200 count+=1 print("Your total money:{:.2f}".format(money)) ``` 結果: ![](https://i.imgur.com/0DYjdQp.png) ::: ## 問三:文字数をsearchする 文字数を調べるプログラムを作成してください。 空白もカウントしないこと。 continueを使うこと。 ``` 入力例: 本文:I love you カウントしない文字:o 出力例: 文字数: 6 入力例: 本文:Prodigious Pokémon Project Piplup plushie provides perfectly peaceful naptime カウントしない文字:P 出力例: 文字数: 65 ``` :::info code: ```python= text=int(input("本文:")) notcount=int(input("カウントしない文字:")) for i in range(text): if i == notcount: continue print("文字数:{}".format()) ``` 結果: ::: ## 問四:Jackpot 数字を当てるゲーム「Jackpot」を作りましょう! これは二人で遊べるゲームで、一人が1から100までの中からJackpotと当てる回数を設定すると、ゲームが開始します。そして、もう一人は数字を入力して当ててみればいいです! **Ameria**: Hi! 解説を担当するAmeriaです。プログラムの最初はJackpotと当てる回数が設定できるようにしてください。設定し終わったら、ゲームが開始します。 次に、指定した回数にだけ、当てる数字を入力できるようにしてください。当たったら、メッセージを表示します(出力例から確認してください) また、もし入力した数字がJackpotより大きいのであれば、数字の範囲の上限を「入力した数字」にして表示してください。逆に、小さい場合は、範囲の下限を入力した数字にしてください。 さらに、範囲を超えた数字を入力したとき、警告のメッセージを表示しましょう! 最後に、試行回数分当ててみても当たらないのであれば、You lost.....を表示しましょう。 ``` 入力例と出力例1: ジャックポット:87 回数制限:4 「-----ゲーム開始-----」 1~100の数字から一つ選んでください 1回目:20 -------------------------- 20~100の数字から一つ選んでください 2回目:90 -------------------------- 20~90の数字から一つ選んでください 3回目:100 I told you enter a number from 20 to 90!! -------------------------- 20~90の数字から一つ選んでください 4回目:35 -------------------------- You lost..... 入出力例2: ジャックポット:55 回数制限:3 「-----ゲーム開始-----」 1~100の数字から一つ選んでください 1回目:54 -------------------------- 54~100の数字から一つ選んでください 2回目:56 -------------------------- 54~56の数字から一つ選んでください 3回目:55 Jackpot is 55. You win!! 例3: ジャックポット:60 回数制限:5 「-----ゲーム開始-----」 1~100の数字から一つ選んでください 1回目:33 -------------------------- 33~100の数字から一つ選んでください 2回目:12 I told you enter a number from 33 to 100!! -------------------------- 33~100の数字から一つ選んでください 3回目:85 -------------------------- 33~85の数字から一つ選んでください 4回目:93 I told you enter a number from 33 to 85!! -------------------------- 33~85の数字から一つ選んでください 5回目:60 Jackpot is 60. You win!! ``` :::info code: ```python= time=1 best=100 last=1 jackpot=int(input("ジャックポット:")) limit=int(input("回数制限:")) print("「-----ゲーム開始-----」") print("1~100の数字から一つ選んでください") num=int(input("{}回目:".format(time))) while time!=0: if jackpot == num and limit == time: print("Jackpot is {}. You win!!".format(num)) elif jackpot != num and limit <= time: print("--------------------------") print("You lost.....") elif jackpot != num and limit >= time: num=int(input("{}回目:".format(time))) print("--------------------------") if num > jackpot: print("{}~{}の数字から一つ選んでください".format(last,num)) num=best time+=1 elif num < jackpot: print("{}~{}の数字から一つ選んでください".format(num,best)) num=last time+=1 jackpot = int(input("ジャックポット:")) limit = int(input("回数制限:")) last=1 best=100 print("「-----ゲーム開始-----」") for c in range(limit): print('{}~{}の数字から一つ選んでください'.format(last,best)) num=int(input(str(c+1)+"回目:")) if num<last or num>best: print("I told you enter a number from {} to {}!!".format(last,best)) elif num == jackpot: print("Jackpot is {}. You win!!".format(jackpot)) break elif num>jackpot: best=num elif num<jackpot: last=num print("--------------------------") else: print("You lost.....") ``` 結果: ![](https://i.imgur.com/f5il0Dm.png) :::