Python: Decisions


These examples demonstrate the use of If statements with Python built-in functions.


Example: If statement with function in Python


	def calcBonus(units):
		if units > 1000:
			print("Your bonus will be 10%")
		elif units > 500:
			print("Your bonus will be 5%")
		elif units > 200:
			print("Your bonus will be 2.5%")

	units = int(input("How many units did you sell this month? "))
	calcBonus(units)
	

Output:

  How many units did you sell this month? 700 {user input}
  Your bonus will be 5%


Example: If statement with functions in Python


	def getInput():
		class_type = input("Please enter the class type code: ")
		return class_type

	def displayClassType(class_type):
		if class_type == "D":
			print("Your class type is day")
		elif class_type == "E":
			print("Your class type is evening")
		elif class_type == "O":
			print("Your class type is online")
		elif class_type == "H":
			print("Your class type is hyrid")
		else:
			print("Invalid class code")

	class_type = getInput()
	displayClassType(class_type)
	

Example: If statement with functions in Python


	def displayMenu():
		print("Main Menu")
		print("---------")
		print("1. Look-Up Employee")
		print("2. Update Employee Information")
		print("3. Print Paycheck")
		print("4. Exit")
		choice = input("Please enter choice: ")
		return choice

	def employeeLookUp():
		print("Look-Up Employee")
		employeeID = input("Enter Employee ID: ")
		# Code to query employee ID
		#
		print("Employee ID", employeeID, "information: ")

	def employeeUpdate():
		print("Update Employee Information")

	choice = displayMenu()
	if choice == "1":
		employeeLookUp()
	elif choice == "2":
		employeeUpdate()
	

Example: If statement with function in Python


	def evaScore(score):
		if score == 200:
			print("Perfect game!  Amazing")
		elif score >= 200:
			print("Outstanding game.")
		elif score >= 150:
			print("Very good game.")
		elif score >= 100:
			print("Good game.")
		else:
			print("Keep practicing.")

	score = int(input("What was your bowling score? "))
	evaScore(score)
	

Example: If statement with function in Python


	def displayMedals(ccode):
		if ccode == "US":
			print("The United States won 23 total medals.")
		if ccode == "NO":
			print("Norway won 39 total medals.")
		if ccode == "DE":
			print("Germany won 29 total medals.")
		if ccode == "AN":
			print("Netherlands won 20 total medals.")

	ccode = input("Enter your country code, e.g. US, etc.: ")
	displayMedals(ccode)
	

Example: If statement with functions in Python


	def feetToInches():
		feet = float(input("Please enter number of feet to convert: "))
		inches = feet * 12
		print(feet, "feet is equal to %.1f" % inches, "inches.")

	def cmToInches():
		print("Finish function code here.")

	def metersToFeet():
		print("Finish function code here.")

	def mmToInches():
		print("Finish function code here.")

	# Main Program
	print("Measurement Converter")
	print("=====================")
	print("1. Feet to Inches")
	print("2. Centimeters to Inches")
	print("3. Meters to Feet")
	print("4. Millimeters to Inches")
	print("5. Exit")
	choice = input("Please enter choice (1-5): ")
	if choice == "1":
		feetToInches()
	elif choice == "2":
		cmToInches()
	elif choice == "3":
		metersToFeet()
	elif choice == "4":
		mmToInches()
	elif choice == "5":
		print("Goodbye.")
	else:
		print("Invalid choice.")