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: - def asdf(n):
-
n=str(m)
-
if n==2:
-
a=True
-
elif n==5:
-
a=True
-
elif m[-1:]==2 or 4 or 6 or 8 or 0 or 5:
-
a=True
I also tried: - 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.
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. - >>> m = 12
-
>>> m % 2
-
-
>>> m % 5
-
2
-
>>> if not m % 2:
-
... print "m is a multiple of 2"
-
...
-
m is a multiple of 2
-
>>>
To check multiple conditions in one statement: - >>> m = 15
-
>>> if not m % 2 or not m % 5:
-
... print "m is a multiple of 2 or m is a multiple of 5"
-
...
-
m is a multiple of 2 or m is a multiple of 5
-
>>> not m % 2 or not m % 5
-
True
-
>>> not m % 2
-
False
-
>>> not m % 5
-
True
-
>>>
A variable can also be checked for membership: - >>> m in [3,6,9,12,15]
-
True
-
>>>
Thank you, but I forgot to say that this funcion will recieve huge numbers, and the method with the module would be very inefficient.
Oralloy 988
Recognized Expert Contributor
@Gonzalo Gonza - build composite conditions, something like this: -
def asdf(n):
-
if (((n % 2) == 0) or ((n % 5) == 0)):
-
result = True
-
else:
-
result = False
-
-
def fdsa(n)
-
result = (((n % 2) == 0) or ((n % 5) == 0))
-
If I've a syntax error, please forgive me; I'm looking at python and trying to learn.
dwblas 626
Recognized Expert Contributor
Note that the variable "m" in not known to the function
Also - elif m[-1:]==2 or 4 or 6 or 8 or 0 or 5:
-
#
-
# m would have to be a string or list to do a [-1], so compare
-
# to strings (compare to integers is m is a list of integers)
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
by: d.schulz81 |
last post by:
Hi all
how can i manipulate a multiple select into a single select dropdown
field with JavaScript?
thanks
|
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,...
|
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...
| |
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) {
|
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'),...
|
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...
|
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 >...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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,...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |