# List(2) - Exercise ###### tags: `Python HomeWork` **例**:着ぐるみのバイトの求人には幾つの条件があります。特に着ぐるみ自体の大きさが決まっているので身長制限があります。とある会社の求人には、160センチ以下が好ましいと書いていて、もし153センチの杏奈が応募したら果たしてTrueなのかFalseなのか、プログラミングで出力してください。 ``` 杏奈の身長を表す変数を利用して比較してください annaHigh = 153 出力例: True ``` :::info code: ```python= annaHigh = 153 print( annaHigh <= 160 ) ``` 答え: ![](https://i.imgur.com/SM2qRLJ.png) ::: -------------------------------------------- ## 問1: ユーザーに入力させ「-1」が入力されるまで、その後にユーザーが入力した数字と入力した回数を出力表示すること。 * functionを使用してください ``` 入力出力例: input: 77 input: 283 input: 765 input: 44 input: -1 List: [77, 283, 765, 44] len: 4 ``` :::info code: ```python= list_empty=[] num=0 while num != -1: num=int(input("input: ")) list_empty.append(num) else: list_empty.remove(int(-1)) print("List: ",list_empty) print("len: {}".format(len(list_empty))) ``` 結果: ![](https://i.imgur.com/gV2pGPd.png) ::: ## 問2: 乱数で1~100の整数を10個生成し出力表示させ、その中での最大値と最小値を表示させること * リストを使う事 * ヒント:平均数値を小数点以下2桁まで表示する事 ``` 入力出力例1: 17 65 4 32 97 73 10 34 63 68 AVERAGE = 46.3 MAX = 97 MIN = 4 入力出力例2: 41 87 12 62 81 94 22 75 3 55 AVERAGE = 53.2 MAX = 94 MIN = 3 ``` :::info code: ```python= import random random_num=10 total=0 list_empty=[] for i in range(random_num): n=random.randint(1,100) list_empty.append(n) total+=n for i in list_empty: print(i,end=" ") print() def n_max(random_num): return max(list_empty) def n_min(random_num): return min(list_empty) def avg(list_empty): return print("AVERAG = {:.2f}".format(total/10)) avg(list_empty) print("MAX = ",n_max(random_num)) print("MIN = ",n_min(random_num)) ``` 結果: ![](https://i.imgur.com/gfxEdJ6.png) ::: ## 問3: 「end」が入力されるまで、ユーザーにリストの内容を入力させます。 リストを逆の順序で出力します。 ``` 入力出力例: input: python input: c++ input: html input: php input: end original: ['python', 'c++', 'html', 'php'] reverse: ['php', 'html', 'c++', 'python'] ``` :::info code: ```python= list_empty=[] list_new=0 while True: languages=input("input: ") if languages=="end": break list_empty.append(languages) list_new+=1 print("original: ",list_empty) list_empty.reverse() print("reverse: ",list_empty) ``` 結果: ![](https://i.imgur.com/F7dn6Lm.png) ::: ## 問4: 欲しい乱数の数(n)を入力し、1から9までのn個の乱数を生成し、最後に1から9までの各数字が何個あるかを数えます。 ``` 入力出力例: input n: 30 list: [5, 8, 2, 5, 8, 5, 2, 4, 7, 3, 5, 4, 1, 7, 8, 9, 7, 1, 4, 8, 2, 6, 9, 8, 8, 2, 6, 2, 6, 3] 1: 2 2: 5 3: 2 4: 3 5: 4 6: 3 7: 3 8: 6 9: 2 ``` :::info code: ```python= """ import random list_empty=[] list_count=[0]*9 input_num=int(input("input n: ")) for i in range(input_num): n=random.randint(1,9) list_count[n-1]+=1 list_empty.append(n) print("list: {}".format(list_empty)) print("{}: {}".format(1,list_empty.count(1))) print("{}: {}".format(2,list_empty.count(2))) print("{}: {}".format(3,list_empty.count(3))) print("{}: {}".format(4,list_empty.count(4))) print("{}: {}".format(5,list_empty.count(5))) print("{}: {}".format(6,list_empty.count(6))) print("{}: {}".format(7,list_empty.count(7))) print("{}: {}".format(8,list_empty.count(8))) print("{}: {}".format(9,list_empty.count(9))) """ import random list_empty=[] input_num=int(input("input n: ")) for i in range(input_num): n=random.randint(1,9) list_empty.append(n) print("list: {}".format(list_empty)) for i in range(1,9): print("{}: {}".format(i,list_empty.count(i))) ``` 結果: ![](https://i.imgur.com/OUSH1Xi.png) ::: ## 問5:sort: 乱数を8つ(範囲:1~99)生成しそれをリストに格納し、それを小さい数字から大きい数字の順に並び替えを行うこと。 ``` 入力例出力: [57, 4, 32, 51, 40, 7, 38, 79] sort: [4, 7, 32, 38, 40, 51, 57, 79] ``` :::info code: ```python= """ import random list_empty=[] MIN=0 for i in range(8): n=random.randint(1,99) list_empty.append(n) print(list_empty) """ import random list_empty=[] MIN=0 for i in range(8): n=random.randint(1,99) list_empty.append(n) print(list_empty) new_list=[] for i in range(8): MIN=min(list_empty) new_list.append(MIN) list_empty.remove(MIN) print("sort: ",new_list) ``` 結果: ![](https://i.imgur.com/X1RTPA9.png) :::