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 trial
Brandon Wall
5,512 PointsConditional if statement using a type e.g. if x == int: Help please.
I'm re-creating the number game completely from scratch without looking at any videos and trying my best to only use python's help function. I want to create an if statement like this
if our_num == int:
break
#So far i'm having to settle for this
if our_num >= 0:
break
This is for a while loop with a try and except block basically letting them choose the difficulty of the game. If they don't enter an int, I have the computer give an angry message saying basically ' don't mess with me, enter a real number' and then loop back to the message prompting them to choose difficulty but i want the break to function based on whether or not the difficulty variable is successfully casted to an int type.
2 Answers
Jeffrey Ruder
6,408 PointsAnother way to do this is:
if isinstance(a, int):
#do stuff
Brandon Wall
5,512 PointsI actually just figured it out: <br> <br> <br>
a = 4
while True:
print("Hello World")
if type(a) == int:
break
<br> <br> Oh god i'm so bad at this formatting thing, sorry this is my first post.
Didn't want to create a new post to play around with formatting stuff, so I'm just editing my post here and playing around with markdown syntax. :P don't mind me.
Brandon Wall
5,512 PointsBrandon Wall
5,512 PointsAwesome, I like that way too. I shall experiment with it. Thank you :D