codescracker


python

Python Decision Making



« Previous Tutorial Next Tutorial »


Decision making statements in python, helps in controlling your program accordingly. There are the following decision making statements available in Python:

All the above three decision making statements are described in a single tutorial. Therefore, all the three links goes to a single tutorial page.

Python Decision Making Example

Here is an example, demonstrating decision making statements in python:

# Python Decision Making - Example
		
while True:
	print("Enter 'x' for exit.")
	val = input("Enter a number: ")
	if val == 'x':
		break
	else:
		numb = int(val)
		if numb>0 and numb<100:
			if numb>0 and numb<50:
				print("You entered number between 0-50.\n")
			else:
				print("You entered number between 50-100.\n")
		else:
			print("You entered number other than 0-100.\n")

Here is the sample run of the above python decision making program:

python decision making

Let's take another example also illustrates how to use decision making statements to control your program:

# Python Decision Making - Example
		
while True:
	print("Enter 'x' for exit.")
	n = input("Enter any number: ")
	if n == 'x':
		break
	else:
		val = int(n)
		if val>0 and val<100:
			if val>0 and val<25:
				print("You entered a number between 0-25.\n")
			elif val == 25:
				print("You entered 25.\n")
			elif val>25 and val<50:
				print("You entered a number between 25-50.\n")
			elif val == 50:
				print("You entered 50.\n")
			elif val>50 and val<75:
				print("You entered a number between 50-75.\n")
			elif val == 75:
				print("You entered 75.\n")
			else:
				print("You entered a number between 75-100.\n")
		elif val == 0:
			print("You entered 0.\n")
		elif val == 100:
			print("You entered 100.\n")
		else:
			print("You entered either negative or a number greater than 100.\n")

Here are snapshots of sample runs of above python program to understand completely about decision making statements in python. This is the first snapshot:

decision making statements in python

Here is the second snapshot:

decision making example program

More Examples

Here are the list of some more examples in python, demonstrates decision making statements in python:


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test