Posts: 4
Threads: 4
Joined: Oct 2018
what should i use for the elif statement to get number not in the list
import random
def rollBall():
system = [0,9,20,24,25,35]
spin = random.randint(0,36)
if spin in system:
return True
elif spin ??? system:
return False
Posts: 8,165
Threads: 160
Joined: Sep 2016
Nov-05-2018, 01:03 PM
(This post was last modified: Nov-05-2018, 01:39 PM by buran.)
import random
def rollBall():
system = [0,9,20,24,25,35]
spin = random.randint(0,36)
return spin in system
Posts: 443
Threads: 1
Joined: Sep 2018
You really don't need the elif. You could use a else instead because the initial logic statement is binary: the number either is or is not in the list. Since there are no other possibilities, you only require the one test at the beginning. If that test fails, then the number is evidently not in the list.
With that out of the way, you could use @buran's answer or you could use the keyword "not":
import random
def rollBall():
system = [0,9,20,24,25,35]
spin = random.randint(0,36)
if spin in system:
return True
elif spin not in system:
return False
Posts: 536
Threads: 0
Joined: Feb 2018
Nov-06-2018, 12:17 AM
(This post was last modified: Nov-06-2018, 12:19 AM by woooee.)
No elif is necessary, as you return True if found and code exits there
import random
def rollBall():
system = [0,9,20,24,25,35]
spin = random.randint(0,36)
if spin in system:
return True
## if no return above then num not in system
return False
Posts: 2,953
Threads: 48
Joined: Sep 2016
The return False is useless because the function returns None if the if condition isn't met. None evaluates to False.
Posts: 8,165
Threads: 160
Joined: Sep 2016
Nov-06-2018, 07:57 AM
(This post was last modified: Nov-06-2018, 07:58 AM by buran.)
(Nov-06-2018, 04:06 AM)wavic Wrote: The return False is useless because the function returns None if the if condition isn't met. None evaluates to False. Although it's true that None is evaluated to False your suggestion has at least 2 weak points - (i) you don't know how the value returned from function is used later on in the code and there might be undesired side-effects when return None instead of False; and (ii) Explicit is better than implicit.
I would always prefer my original suggestion, but for completeness, one more possible code snippet is
if spin in system:
return True
else:
return False i.e. there is no need for second evaluation.
Posts: 4,798
Threads: 77
Joined: Jan 2018
Nov-06-2018, 08:51 AM
(This post was last modified: Nov-06-2018, 08:51 AM by Gribouillis.)
You can also use
return bool(spin in system) This makes it explicit that the return value is True or False , although this is probably already the case with the in operator.
Posts: 8,165
Threads: 160
Joined: Sep 2016
(Nov-06-2018, 08:51 AM)Gribouillis Wrote: You can also use
return bool(spin in system) This makes it explicit that the return value is True or False , although this is probably already the case with the in operator. spin in system is already a bool, no need to cast it
|