2017-10-24 3 views
0

ユーザーの入力値のリストを5の倍数で確認するにはどうすればよいですか?私は、これらの値の何パーセントを印刷できるようにする必要があります例えば5の倍数である:ここでPython 3 - ユーザー入力のリストを5の倍数で確認する

intList = [5, 10, 11, 15] 
"75% of values in intList are multiples of 5" 

は私のコードは、これまでのところです:

intList = [] 

running = True 
while running: 
    intAmount = int(input("Enter the amount of integers you are inputting: ")) 
    if intAmount > 0: 
    running = False 


for i in range (intAmount): 
    integers = int(input("Enter an integer here: ")) 
    intList.append(integers) 
print(intList) 

答えて

1

はコード:

intList = [int(x) for x in input('Enter list of numbers: ').split()] 
count = 0 

for num in intList: 
    if (num % 5) == 0: 
     count+=1 

percent = (count/len(intList)) * 100 
print("%.2f%% of values in intList are multiples of 5"%percent) 

入力:

separated by spaceを入力してください。

Enter list of numbers: 4 6 2 10 9 45 

出力:

33.33% of values in intList are multiples of 5 

コード2: (ユーザが要求した通り)

intList = [] 

running = True 
while running: 
    intAmount = int(input("Enter the amount of integers you are inputting: ")) 
    if intAmount > 0: 
     running = False 


for i in range (intAmount): 
    integers = int(input("Enter an integer here: ")) 
    intList.append(integers) 
print(intList) 

count = 0 
for num in intList: 
    if (num % 5) == 0: 
     count+=1 

percent = (count/len(intList)) * 100 
print("%.2f%% of values in intList are multiples of 5"%percent) 
+0

私は私の現在にその機能を使用することができる方法はあります私は整数を入力する方法を変更_without_プログラム? – FrostBite

+0

はい、できます。 (NUM%5)== 0の場合 ::ちょうどintListでnumの= 0 '数を追加します。 数+ = 1 パーセント=(カウント/ LEN(intList))* 100 印刷("%2F intListの値の%%は5 "%percent%の倍数です)'あなたが質問の中でコードしていることは、そのトリックを行うべきです。 –

+0

私はあなたのために答えに 'code 2'セクションを追加しました。 –

関連する問題