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
Jason Ladieu
10,635 PointsChecking multiple conditions in one conditional statement question.
Hi there,
I'd like to test two conditions using one conditional statement (if possible) in the first_num and second_num functions. I can't figure out how to make the condition I have fail if a string is entered. I'd also like the condition to pass if float is entered. For this I think I would need two conditional statements, but if it can be in one it would be better! Any help is much appreciated !
# My Calculator Script: version 1.0
# Collect first number and check for integer or float
def first():
global first_num
first_num = int(input("Please enter the first number:\n"))
if first_num == 0:
print("Error, please choose a value other than 0.\n")
first()
# Collect and check for operator
def operators():
global enter_op
enter_op = input("Please enter an operator. You can choose: -, +, /, or *.\n")
enter_op = str(enter_op)
if enter_op not in ("-", "+", "/", "*"):
print("Error, please enter a valid operator. You can choose from -, +, /, or *.\n")
operators()
# Collect second number and check for integer or float
def second():
global second_num
second_num = int(input("Please enter the second number:\n"))
if second_num == 0:
print("Error, please choose a value other than 0.\n")
second()
second_num = int(second_num)
#solve and print
def solution():
if enter_op == ('+'):
print('{} + {} = '.format(first_num, second_num))
print(first_num + second_num)
elif enter_op == ('-'):
print('{} - {} = '.format(first_num, second_num))
print(first_num - second_num)
elif enter_op == ('/'):
print('{} / {} = '.format(first_num, second_num))
print(first_num / second_num)
elif enter_op == ('*'):
print('{} * {} = '.format(first_num, second_num))
print(first_num * second_num)
else:
print("What happened!")
#execute function
def solve():
first()
operators()
second()
solution()
solve()
1 Answer
Nathan Tallack
22,164 PointsThe beauty of Python is that it reads well.
Consider:
if True or True:
print("This is true!")
if True or False:
print("This is also true!")
if True and False:
print("This is false!")
if (True and False) or (True and True):
print("This is true!")
Python is awesome! :)
Jason Ladieu
10,635 PointsJason Ladieu
10,635 PointsThanks for the answer! How can I make the condition fail if a string was entered ?
Nathan Tallack
22,164 PointsNathan Tallack
22,164 PointsIf you want have the condition test if it is of type string then you can do this.
...etc.
So what you are using here is an inbuilt python function called isinstance which takes the object or literal that you want to test as the first parameter and the type as the second parameter returning True or False accordingly.