In Part 2 for Python3 Basics for beginners blog, we read about Input function, Return and Casting. In this blog, we will advance our understanding of python by learning conditional coding techniques like if else and nesting.
1. IF/Else Conditions
If/Else plays important role in evaluating user inputs from input functions and then determining whether input need to processed by function or should be rejected and thrown as warning to user. It basically uses Operators like Comparison (<,>,== ..) for resulting action.
Code
good_coffee=input("Which Coffee you like?\n")
bad_coffee=input("Which Coffee you don't like?\n")
how_many=input("How much you like your coffee?\n")
def coffee(many_times):
if many_times > 0:
return f"{good_coffee} is {how_many} times better than {bad_coffee}"
else:
return f"Cannot be negative value"
many_times = int(how_many)
output_comment = coffee(many_times)
print(output_comment)
In example above, I appended our previous code of determining which coffee is better but inserted if,else conditions to stop user from answering negative value when comparing
Output
Which Coffee you like?
Black
Which Coffee you don't like?
Latte
How much you like your coffee?
140
Black is 140 times better than Latte
Which Coffee you like?
Black
Which Coffee you don't like?
Latte
How much you like your coffee?
-5
Cannot be negative value
2. Nesting
By now, my code is working absolutely fine but its getting bigger and messier. To make code looks neater, we can nesting technique which basically can combine two functions to merge in single line. For example, I am asking my final print output to call function, which inturn calling input integer converted value.
Code
*********Normal**********
many_times = int(how_many)
output_comment = coffee(many_times)
*********Nested********
output_comment=coffee((int(how_many)))
3. Advanced If/Else
In simple, If/Else coding, we only worked If value is greater or lesser, but what is user inputs value in form of text or floating number, where my code was written for processing integer only. It can disrupt my code and throw error.
To avoid it, we can use another if/else statement in my code, where I specify proceed for processing only when input provided is integer or whatever my code need itself to run.
Code
good_coffee=input("Which Coffee you like?\n")
bad_coffee=input("Which Coffee you don't like?\n")
cost=input("How much it cost?\n")
def coffee(costing):
if costing > 0:
return f"{good_coffee} is better than {bad_coffee}, even though it costs {costing} Dollars"
elif costing == 0:
return f"Price cannot be Zero"
else:
return f"Price cannot be negative"
if cost.isdigit():
output=coffee((int(cost)))
print(output)
else:
print("Input is invalid")
Output
In above example, we used two advanced If/Else functions, “elif” and “isdigit()”. While elif is used when I have more than one conditions for my code to process, isdigit actually allowed my Code to only process input if value is digit or integer only.
Which Coffee you like?
Black
Which Coffee you don't like?
Latte
How much it cost?
two
Input is invalid
Which Coffee you like?
Black
Which Coffee you don't like?
Latte
How much it cost?
15
Black is better than Latte, even though it costs 15 Dollars
Checkout our next blog in this python3 learning series to know more about using python language.