0

In my code, I choose 3 numbers in my list. Then I want it to see if there are any multiples of 3 in the list. When I run it, it shows all the multiples of 3 but also runs the else too.

How can you only run the else if there is no multiple of 3?

array = []

for x in range(3):
    nums = int(input("What are your numbers? "))
    array.append(nums)

print(array)


for i in array:
    if i % 3 == 0:
        print(i, "Is Multiple of 3")

else:
    print("no multiple")

run code,

What are your numbers? 23
What are your numbers? 33
What are your numbers? 6
[23, 33, 6]
33 Is Multiple of 3
6 Is Multiple of 3
no multiple

Process finished with exit code 0
killashot
  • 17
  • 6
  • You seem to misunderstand (or not realize) that `for-else` is a feature of Python – OneCricketeer Feb 01 '17 at 05:23
  • What exactly you want? If all numbers are not multiple of 3 then should print or if single element is not multiple of 3, then you have to print? – Harsha Biyani Feb 01 '17 at 05:26
  • You need some kind of flag. Before your loop, set the flag to `False`, and inside your `if`, set it to `True`. After your loop, add another `if` to test if your flag is still `False` (ie you never found a multiple). – Kevin Feb 01 '17 at 05:26
  • You could use `if not any(x % 3 == 0 for x in array): print() else: ...` – OneCricketeer Feb 01 '17 at 05:27

0 Answers0