Python 3 - Проверьте список пользовательских входных данных для кратных 5
Как я могу проверить список введенных пользователем значений для кратных 5? Мне нужно иметь возможность напечатать, какой процент этих значений кратны 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)