Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialNicolás Tudor
744 PointsPickpocket function
Hi, I have a question about the conditional inside the pickpocket function. Why omit the use of else? The conditional still works, but I don't know exactly why. I would have written it like this:
def pickpocket(self):
if self.sneaky: #Can this also be written as: if self.sneaky == True: ?
return bool(random.randint(0,1))
else:
return False
3 Answers
Ave Nurme
20,907 PointsHi Nicolas!
Use 3 backticks (```) on the line before and after to wrap your code (check the Markdown Cheatsheet for this). This way it will display correctly and it's much easier for others to read. There is also a preview button if you wish to preview your post before posting.
Regarding your question:
if self.sneaky == True:
does work but if self.sneaky:
is much simplified version of the expression. As I tried this out in PyCharm it also suggested to use the shorter version.
You can use the else-statement if you wish to but it is really not necessary as thanks to the indentation Python already knows what to do if if self.sneaky:
is False (in other words, if the condition is not met).
I'm no expert of course and just a regular student :-)
Ave Nurme
20,907 PointsAs I have learned so far indentation in Python is very important so when you write code you should always pay attention to this.
Actually I suggest you play around both with print
and return
. With print
the else
clause is actually needed, otherwise you might get unexpected results. At least this is something what I have experienced.
return
ends the execution of the function call even if it is not the last statement of the function but print
just prints out everything as long as there is someting to print.
Here's a few code examples with print
I wrote for you. You can try them out - feel free to copy-paste them! Try to catch the difference. The first two examples are without the else
clause but the last two have else
clause in them.
When I started learning I had these concepts mixed and didn't really see the difference at first. Maybe you already know all of this :-)
sneaky = True
def s():
if sneaky:
print(1)
print(0)
s()
sneaky = False
def s():
if sneaky:
print(1)
print(0)
s()
sneaky = True
def s():
if sneaky:
print(1)
else:
print(0)
s()
sneaky = False
def s():
if sneaky:
print(1)
else:
print(0)
s()
Nicolás Tudor
744 PointsThank you, it did clear some things up. I tried something else from your code:
sneaky = True
def s():
if sneaky:
print(1)
return 0
print(s())
#This code returns 1 and 0, return does not take else for granted.
Here is the same code but with else implemented:
sneaky = True
def s():
if sneaky:
print(1)
else:
return 0
print(s())
#This returns 1 and "none". Strange, isn't it?
Nicolás Tudor
744 PointsI apologize for the mess, I thought the post would show up the way I wrote it, with tabs and spaces...
Nicolás Tudor
744 PointsNicolás Tudor
744 PointsThank you, that did solve the text formatting problem. I find very curious that python indentation already takes else for granted.
What else can python indentation do?