473,498 Members | 1,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple conditions in a single IF

16 New Member
I want to know whether a number is a multiple of 2 and/or 5
But Python doesn't let me put lots of things after the '=='

Here's my code:
Expand|Select|Wrap|Line Numbers
  1. def asdf(n):
  2.     n=str(m)
  3.     if n==2:
  4.         a=True
  5.     elif n==5:
  6.         a=True
  7.     elif m[-1:]==2 or 4 or 6 or 8 or 0 or 5:
  8.         a=True
I also tried:
Expand|Select|Wrap|Line Numbers
  1. elif m[-1:]==2, 4, 6, 8, 0, 5:
But it doesn't make sense

I wish you could help me
Thank you

PS: I know that the program don't make any sense but that's not the real purpose, it's just to simplify.
Nov 16 '10 #1
4 19328
bvdet
2,851 Recognized Expert Moderator Specialist
To determine if a variable is a multiple of 2 or 5, use the modulo operator.
Expand|Select|Wrap|Line Numbers
  1. >>> m = 12
  2. >>> m % 2
  3.  
  4. >>> m % 5
  5. 2
  6. >>> if not m % 2:
  7. ...     print "m is a multiple of 2"
  8. ...     
  9. m is a multiple of 2
  10. >>> 
To check multiple conditions in one statement:
Expand|Select|Wrap|Line Numbers
  1. >>> m = 15
  2. >>> if not m % 2 or not m % 5:
  3. ...     print "m is a multiple of 2 or m is a multiple of 5"
  4. ...     
  5. m is a multiple of 2 or m is a multiple of 5
  6. >>> not m % 2 or not m % 5
  7. True
  8. >>> not m % 2
  9. False
  10. >>> not m % 5
  11. True
  12. >>> 
A variable can also be checked for membership:
Expand|Select|Wrap|Line Numbers
  1. >>> m in [3,6,9,12,15]
  2. True
  3. >>> 
Nov 16 '10 #2
Gonzalo Gonza
16 New Member
Thank you, but I forgot to say that this funcion will recieve huge numbers, and the method with the module would be very inefficient.
Nov 16 '10 #3
Oralloy
988 Recognized Expert Contributor
@Gonzalo Gonza - build composite conditions, something like this:

Expand|Select|Wrap|Line Numbers
  1. def asdf(n):
  2.   if (((n % 2) == 0) or ((n % 5) == 0)):
  3.     result = True
  4.   else:
  5.     result = False
  6.  
  7. def fdsa(n)
  8.   result = (((n % 2) == 0) or ((n % 5) == 0))
  9.  
If I've a syntax error, please forgive me; I'm looking at python and trying to learn.
Nov 16 '10 #4
dwblas
626 Recognized Expert Contributor
Note that the variable "m" in not known to the function
Expand|Select|Wrap|Line Numbers
  1. def asdf(n):
  2.     n=str(m)
Also
Expand|Select|Wrap|Line Numbers
  1.     elif m[-1:]==2 or 4 or 6 or 8 or 0 or 5:
  2. #
  3. #   m would have to be a string or list to do a [-1], so compare 
  4. #   to strings  (compare to integers is m is a list of integers)
  5.     elif m[-1] in ['2', '4', '6', '8', '0', '5']: 
[2, 4, 6, 8, 0, 5] should give you all numbers divisible by 2 or 5, I think.
Nov 17 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
66843
by: M Wells | last post by:
Hi All, Just wondering if anyone can tell me if you can test for multiple conditions as part of an "IF" statement in T-SQL in SQL Server 2000? ie something like: IF @merr = 1 or @merr=2...
2
2815
by: Ginu | last post by:
Hi, the task is to identify semantically identical elements where some additional attributes do not match. The XSL-transformation should find a node NAME which @id attribute matches to another...
3
5620
by: d.schulz81 | last post by:
Hi all how can i manipulate a multiple select into a single select dropdown field with JavaScript? thanks
0
2196
by: rayone | last post by:
Hi folks. I need advice. 2 options, which do you think is the better option to display/retrieve/report on the data. Keep in mind reporting (Crystal), SQL Performance, VB Code, usability,...
10
2660
by: greedo | last post by:
Hi, first of all, please bear with me, I don't really know a great deal about php programming, as will be painfully obvious from the code snippet I'm gonna post. But I need this problem solved, and...
13
2063
by: TheLostLeaf | last post by:
Hi, Does anyone know if these 2 code segments would operate the same way. for instance if the first condition fails does it bother to check the second? if (MyList.Count 0 && CheckDate) {
5
50545
by: jorgedelgadolopez | last post by:
Hi all, I am using the xpathnavigator evaluate function on .net (xpath 1 right?). Now I need to expand the code to do multiple contains, compare dates (such as 'before', 'between' and 'after'),...
3
18168
by: kavithapotnuru | last post by:
Hi All, Can anyone help me how to give multiple conditions in a while loop in perl. For example i have a while loop for which the value of a variable is A or B it should not enter in to the...
3
3960
by: bnashenas1984 | last post by:
Hi everyone I'v been looking in google but haven't got any answer for my question yet. Actualy I have a search page which works with mysql. my question is: SELECT * FROM usertable WHERE age >...
0
7124
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6998
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5460
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
651
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.