Python Forum
Issue with affecting numbers to a variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue with affecting numbers to a variable
#1
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")
Reply
#2
(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,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#3
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.
Reply
#4
Thanks for helping , it s finally working!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Float Slider - Affecting Values in Column 'Pandas' planckepoch86 0 1,370 Jan-22-2022, 02:18 PM
Last Post: planckepoch86
  Variable scope issue melvin13 2 1,508 Nov-29-2021, 08:26 PM
Last Post: melvin13
  Variable Issue slackerman73 2 1,883 Nov-09-2019, 04:34 PM
Last Post: slackerman73
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,667 May-09-2019, 12:19 PM
Last Post: Pleiades
  Mixed string,Integer input variable issue maderdash 2 2,714 Nov-06-2018, 09:46 AM
Last Post: snippsat
  Variable Resetting Issue Niicollas__ 8 4,170 Jan-20-2018, 08:52 PM
Last Post: j.crater
  netCDF issue with filling a variable HeavyLoads 1 11,272 Oct-03-2016, 08:34 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020