3.1.1.9 Making decisions in Python
Analyzing code samples
Now we're going to show you some simple yet complete programs. We won't explain them in detail, because we consider the comments (and the variable names) inside the code to be sufficient guides.
All the programs solve the same problem - they find the largest of several numbers and print it out.
Example 1:
We'll start with the simplest case - how to identify the larger of two numbers:
# read two numbers
number1 = int(input("Enter the first number: "))
number2 = int(input("Enter the second number: "))
# choose the larger number
if number1 > number2:
larger_number = number1
else:
larger_number = number2
# print the result
print("The larger number is:", larger_number)
The above snippet should be clear - it reads two integer values, compares them, and finds which is the larger.
Example 2:
Now we're going to show you one intriguing fact. Python has an interesting feature, look at the code below:
# read two numbers
number1 = int(input("Enter the first number: "))
number2 = int(input("Enter the second number: "))
# choose the larger number
if number1 > number2: larger_number = number1
else: larger_number = number2
# print the result
print("The larger number is:", larger_number)
Note: if any of the if-elif-else branches contains just one instruction, you may code it in a more comprehensive form (you don't need to make an indented line after the keyword, but just continue the line after the colon).
This style, however, may be misleading, and we're not going to use it in our future programs, but it's definitely worth knowing if you want to read and understand someone else's programs.
There are no other differences in the code.
Example 3:
It's time to complicate the code - let's find the largest of three numbers. Will it enlarge the code? A bit.
We assume that the first value is the largest. Then we verify this hypothesis with the two remaining values.
Look at the code below:
# read three numbers
number1 = int(input("Enter the first number: "))
number2 = int(input("Enter the second number: "))
number3 = int(input("Enter the third number: "))
# We temporarily assume that the first number
# is the largest one.
# We will verify this soon.
largest_number = number1
# we check if the second number is larger than current largest_number
# and update largest_number if needed
if number2 > largest_number:
largest_number = number2
# we check if the third number is larger than current largest_number
# and update largest_number if needed
if number3 > largest_number:
largest_number = number3
# print the result
print("The largest number is:", largest_number)
This method is significantly simpler than trying to find the largest number all at once, by comparing all possible pairs of numbers (i.e., first with second, second with third, third with first). Try to rebuild the code for yourself.
Comments
Post a Comment