2017-10-24 12 views
0

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) 
나는 예를 들어 5의 배수는이 값의 비율을 인쇄 할 수 있어야합니다

입력 :

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

네, 할 수 있습니다. 다만 IntList를에 NUM위한 '카운트 = 0 를 추가 : 을 경우 (NUM의 5 %) == 0 카운트 + = 1 백분율 = (카운트/LEN (IntList를)) * 100 인쇄 ("% 2F. intList의 값 중 %%는 5 "% percent"의 배수입니다.) 질문에 코드를 작성하면 트릭을 수행해야합니다. –

+0

참조 용으로'code 2'섹션을 추가했습니다. –