Python: Decisions

Introduction

A computer has no intelligence without programming. It is the programming that allows computers to, for example, make decisions. Just as we make decisions in our daily lives based on our rules for living, so to can we program computers to make decisions. The only difference to-date, is our programming has not created computers with human-levels of intelligence and computers are not conscious and self-aware. Two common examples of evaluating whether a computer has reached the level of the "human-level of intelligence" are a) the Turing Test developed by Alan Turing where when a test is administered to a computer it's answers make it indistinguishable from a computer, and b) scene depecting this test being administered in the sci-fi movie Blade Runner.

Nevertheless, there are countless examples of intelligence in computers, e.g. chess strategy and other strategy computer games, autonomous driving vehicle, software that helps individuals or professionals diagnose problems in a domain (sometimes referred to as expert systems), simulation software, autopilot code in airplanes, email spam filtering software the learns and adapts, to name a few.

In a great many computer languages, decisions are programmed using one of three programming constructs. Some languages have all three, while others may only have one or two.

  • if statement
  • switch ... case statement
  • Ternary operator

Python has if statements and the ternary operator, but not a switch statement. Many languages also have a try .. catch statement of error handing, which acts like a decision/if statement. Switch like statements can also be achieved in Python by way of dictionaries.

In this section, we will explore basic if statements. In the second decision menu link, we will explore the use of user-defined functions with if statements. And in the third decision menu link we will explore Python alternatives to the if statement.


Example: If statement in Python

Below is a simple if statement in Python. Note, the double equals sign used for comparisons and the quote marks around Mary Smith because it is a string. Also, a colon must go at the end of any if, elif, or else in Python. Lastly, the print line must be intended or the if statement will not run (a syntax error would occur).


	employee1 = "Mary Smith"

	if employee1 == "Mary Smith":
		print ("Hello Mary!")
	

Output:

  Hello Mary!
  


Example: If-Else statement in Python


	employee1 = "Mary Smith"

	if employee1 == "Mary Smith":
		print ("Hello Mary!")
	else:
		name1 = str(input("Who are you? "))
		print ("Hello", name1)
	

Output:

  Who are you? John {user input}
  Hello John


Example: If statement with a number


	age = 19
	if age >= 18:
		print("You are old enough to vote.")
	

Output:

  You are old enough to vote.


Example: If-Elif-Else


	grade = 84

	if grade > 90:
		print ("You achieved an A")
	elif grade > 80:
		print ("You achieved a B")
	elif grade > 70:
		print ("You achieved a C")
	elif grade > 60:
		print ("You achieved a D")
	else:
	

Output:

  You achieved a B


Example: If-Else with In operation and list


	zipcode_list1 = [48099,48100,48101,482012]
	zipcode1 = 48099
	if zipcode1 in zipcode_list1:
		print("You vote in location 1.")
	else:
		print("You vote in location 4.")
	

Output:

  You vote in location 1.


Example: If-Elif Calculator


	num1 = float(input("Enter first number: "))
	operator = input("Enter operator (+ - * /): ")
	num2 = float(input("Enter second number: "))

	if operator == "+":
		total = num1 + num2
	elif operator == "-":
		total = num1 - num2
	elif operator == "*":
		total = num1 * num2
	elif operator == "/":
		total = num1 / num2

	print (num1, operator, num2, "=", total)
	

Output:

  Enter first number: 5 {user input}
  Enter operator (+ - * /): * {user input)
  Enter second number: 3 {user input}
  5.0 * 3.0 = 15.0


Example: If statement with OR logical operator


	age1=77
	if age1 < 12 or age1 > 65:
		print("You receive a discount on your meal.")
	

Output:

  You receive a discount on your meal.


Example: If statement with AND operator


	age1=25
	age2=21
	age3=32
	if age1 >= 18 and age2 >= 18 and age3 >=18:
		print("You are all old enough to vote.")
	else:
		print("At least one of you is not old enough to vote.")
	

Output:

  You are all old enough to vote.


Example: If statement with logical operators, not and order


	age1=77
	day="Sunday"
	if (age1 < 12 or age1 > 65) and day != "Friday":
		   print("You receive a discount on your meal.")
	

Output:

  You receive a discount on your meal.


Example: Nested If statement with built-in function


	from fnmatch import fnmatch, fnmatchcase

	employee1 = "Mary Johnson"
	salary = 59000

	if fnmatch(employee1, 'Mary*'):
		if salary >= 50000:
			print ("Mary, you make over $50,000.")
		else:
			print ("Mary.  You do not yet make $50,000.")
	else:
		print ("I don't know who you are.")
	

Output:

  Mary, you make over $50,000.


Example: If statement in Python


	zipcode = input("Enter your zip code: ")

	if zipcode == "48180":
		print("You live in Taylor.")
	elif zipcode == "48239" or zipcode == "48240":
		print("You live in Redford.")
	elif zipcode == "48125" or zipcode == "48127":
		print("You live in Dearborn Heights.")
	elif zipcode in ['48120','48121','48123','48126','48128']:
		print("You live in Dearborn.")