Python Forum

Full Version: Issue with affecting numbers to a variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello , i'm trying to code with python, i get little bit of problems with affecting many numbers to a variable. Can someone tell me what is the issue in my code ? How can I solve it ? Thanks for helping

a=[ 2, 4, 8, 17, 18021990]


if a % 4==0:
    print ("I'm a multiple of 4")
(Sep-23-2019, 08:34 PM)Adem Wrote: [ -> ]hello , i'm trying to code with python, i get little bit of problems with affecting many numbers to a variable. Can someone tell me what is the issue in my code ? How can I solve it ? Thanks for helping

a=[ 2, 4, 8, 17, 18021990]


if a % 4==0:
    print ("I'm a multiple of 4")

Hi!

I'm also a newbie.

You cannot operate (like with %) two different types of data, like 'lists' (a) and 'integers' (int), but you can twist it a little, taking one by one, all the elements from the list, like:

a=[ 2, 4, 8, 17, 18021990]
 
 
for x in a:
    if x % 4==0:
        print ("The number", x, "is a multiple of 4.")
that produces the following output:

Output:
The number 4 is a multiple of 4. The number 8 is a multiple of 4.
All the best,
Adem Wrote:Can someone tell me what is the issue in my code ? How can I solve it ?
The issue is that you define a as a list of numbers, which is perfect, but a list of numbers is not a multiple of 4 and the operation a % 4 is not defined for this type.

You can solve it by defining more precisely what you are trying to achieve and the result you're expecting.
Thanks for helping , it s finally working!