Reputation: 137
class Wheels(Car):
def __init__(self, name, noise, canMakeNoise):
self.name = name
self.noise = noise
self.canMakeNoise = Bool
def makeNoise(self):
if canMakeNoise = False:
Wheels.name + "" + "vroom"
else:
print Wheels.name + "" + "remains silent"
If I run that code, I get this error:
File "python", line 26
if canMakeNoise = False:
^
SyntaxError: invalid syntax
How can you put a bool in a if else statement in Python?
Upvotes: 1
Views: 128
Reputation: 180540
You can use a ternary if/else, also using str.format
will make your concatenation a bit nicer:
print"{} remains silent".format(Wheels.name) if canmakeNoise else "{} vroom".format(Wheels.name)
Upvotes: 0
Reputation: 66
def makeNoise(self):
if canMakeNoise:
print Wheels.name + "" + "remains silent"
else:
Wheels.name + "" + "vroom"
Upvotes: 1
Reputation:
You shouldn't. The proper way to do this in Python is:
if not canMakeNoise:
and:
if canMakeNoise:
for the opposite. From PEP 8:
Don't compare boolean values to
True
orFalse
using==
.Yes:
if greeting:
No:if greeting == True:
Worse:if greeting is True:
Just for the sake of completeness, you need to use ==
for comparisons. =
is for assignment.
Upvotes: 1