FOUNDATIONS OF PROGRAMMING : LOGIC IN PROGRAMMING : 04.02 MORE DECISIONS

Ace your homework & exams now with Quizwiz!

Multiple Decisions with elif Statements: The elif Statement

# The elif Statement def main(): a = 10 b = 200 print("Facts about a and b:") if( a == b ): print(" a equals b ") elif( a > b ): print(" a is greater than b ") elif( a < b ): print(" a is less than b ") else: print("There was an error.") main() Output: Facts about a and b: a is less than b

Multiple Decisions with elif Statements: Who Wins?

# The elif Statement def main(): print("Who won the game?") yourScore = 9520 myScore = 9905 if( yourScore > myScore ): print("You win!") elif( yourScore < myScore ): print("I win!") else: print("We tied!") main() Output: Who won the game? I win!

Multiple Decisions with elif Statements: A Menu and While Loop

# The elif Statement import turtle def sunBurst(t): # move to position t.penup() x = 150 y = 130 t.setpos(x, y) #Sets the position of the turtle t.setheading(270) t.pendown() t.color("gold") # draw star t.setpos(x- (x/1.75), y -(y/12)) for n in range(40): t.forward(60) t.left(130) def spiralTwist(t): """This function draws an eye """ t.color("blue") t.penup() t.setpos(-60, 60) t.pendown() for x in range(0, 75, 2 ): t.forward(x) t.left(91) def doily(t): """ This function draws a frown """ t.penup() t.setpos(45, 5) t.left(90) t.pendown() for a in range(2): for b in range(5): t.color("green") t.circle(30) t.circle(20) t.forward(20) for c in range(3): t.color("yellow") t.right(80) t.forward(5) t.forward(40) t.left(36) def printMenu(): """ This function prints the menu to the screen """ print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") print("~ Menu of Turtle Patterns to Draw") print("~ 1: Sunburst") print("~ 2: Spiral Twist") print("~ 3: Doily") print("~ 4: Exit") print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") def main(): t = turtle.Turtle() t.speed(10) choice = "-1" while(choice != "4"): printMenu() choice = input("Look at the menu. Which item would you like to draw? 1, 2, or 3. Select 4 to exit. ") if(choice == "1"): sunBurst(t) elif(choice == "2"): spiralTwist(t) elif(choice == "3"): doily(t) elif(choice == "4"): print("Thank you for drawing today.") else: print("Oops, an invalid option was picked. Please try again.") main() Output: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~ Menu of Turtle Patterns to Draw ~ 1: Sunburst ~ 2: Spiral Twist ~ 3: Doily ~ 4: Exit

Decisions with if-else Statements: Which Movie?

# The if-else Statement def main(): show = input("When is the movie? afternoon or evening: ") if(show == "afternoon"): ticketCost = 5.50 else: ticketCost = 8.00 print ("Cost of tickets: $" + str(ticketCost)) main() Output: Cost of tickets: $5.5

Decisions with if-else Statements: The if-else Statement

# The if-else Statement def main(): x = 20 y = 30 print("Facts about a and b:") if( x == y ): print(" x equals y ") else: print(" x is not equal to y ") main() Output: Facts about a and b: x is not equal to y

Decisions with if-else Statements: Decisions with Turtles

# The if-else Statement import turtle def drawFace(t): # This function draws a face. t.color("yellow") t.penup() t.setpos(0, -30) t.pendown() t.begin_fill() t.circle(80) t.end_fill() def drawEyes(t): # This function draws an eye. t.color("blue") t.penup() t.setpos(30, 65) t.pendown() t.begin_fill() t.circle(15) t.end_fill() t.penup() t.setpos(-30, 65) t.pendown() t.begin_fill() t.circle(15) t.end_fill() def drawFrown(t): # This function draws a frown. t.color("red") t.width(10) t.penup() t.setpos(42, 0) t.left(90) t.pendown() t.begin_fill() t.circle(40, 180) t.end_fill() def drawSmile(t): # This function draws a smile. t.color("red") t.width(10) t.penup() t.setpos(-38, 30) t.right(90) t.pendown() t.begin_fill() t.circle(40, 180) t.end_fill() def main(): t = turtle.Turtle() t.speed(10) drawFace(t) drawEyes(t) smile = input(" Do puppies make you smile? y/n: ") if(smile == "y"): drawSmile(t) else: drawFrown(t) main() Output: turtle graphtic frownig face

if statements

With if statements, if the condition is true, you take an action. If it is false, you do nothing. For example, if you win a race, you get a medal. If you don't win, you get...nothing. However, more often than not, you'll want to write programs that can handle multiple decisions. This is where if-else and elif statements come in handy. Check out how these two statements work with multiple decisions. With if-else statements, if the condition is true, you take an action. If it is false, you take the else action. Example: if you win a race, you get a medal. if you don't win, you get a participation ribbon. elif statements allow you to check multiple conditions. As soon as one is determined to be true, the action related to that statement is taken. Example: if you come in first place in a race, you get a first place medal. elif you come in second, you get a second place medal. elif you come in third, you get a third place medal. else you get a participation ribbon. Depending on the program you are writing, you'll want to use the decision statement that matches the level of decision-making you want it to execute. For simple decisions, use the if statement. For two options, use the if-else statement. And when you need to check many possibilities, use elif.

elif Statements

elif Statements teenageer with a report card © Shutterstock.com Here's the thing: sometimes you need more than two options. This is where elif statements come in. Think of elif as a combination of the words else and if. In an elif statement, you can allow for a wider range of possibilities. Here's an example: grade = input("What is your grade?") if( grade == "A" ): print("Excellent!") elif( grade == "B" ): print("Good job!") elif( grade == "C" ): print("Nice start. Try to study more next time!") else: print("Please reach out to your teacher.") Here, you've asked a user to input his or her grade. Python will compare the response with the options in the code A, B, and C in order, one at a time. It will use Boolean logic to determine whether each string matches the response. As soon as Python finds a match, it will stop checking, and it will print the appropriate response. In other words, if the user enters A, then Python won't check the elif conditions in the code. It will find a match right away and print Excellent! However, if the user enters C, Python will check A, then B, and finally get to C, before it finds a true condition. Did You Know? With elif statements, Python may or may not take any action. if(choice == "x"): do this if true elif(choice == "y"): do that if true elif(choice == "z"): do the other thing if true else: default thing if nothing else is true What if the user enters m instead of the x, y, or z options? Since an else statement is included, it will catch all "other" options and produce a default action. But if the else statement were removed, no action would be taken.

Decisions with if-else Statements: Heads or Tails?

from random import * def main(): flips = int( input(" Enter the number of coin flips: ")) heads = 0 tails = 0 for count in range(flips): result = random() # a random number from 0.0 to less than 1.0 if(result < 0.5): heads = heads + 1 else: tails = tails + 1 print("") print("Coin Toss Results") print("=========================") print("Heads: " + str(heads)) print("Tails: " + str(tails)) main() Output: Coin Toss Results ========================= Heads: 2 Tails: 3

if-else Statements

if-else Statements vector war game interface technology © Shutterstock.com Life would get pretty boring if you had only one option for everything you wanted to do. That's why you get to make choices. For example, if you wanted to play a game solo, you could choose to play offline. Or else you can select the online option. Python has a pretty easy setup for this if-else scenario. It uses Boolean logic to determine whether a condition is true. If it is true, the program executes the block of code following the if statement. If it's not true, the program moves to the else and executes that block of code. Let's take a look: age = int(input("What is your age?")) if(age >= 17): print("Enjoy the movie!") else: print("Please bring a parent or guardian.") In this code, you're asking the user to enter his or her age before buying a ticket to a movie with age restrictions. Using the if statement, Python will compare the age with the number 17 and use Boolean logic to determine whether it is greater than or equal to 17. if the age is 17 or higher, Python will print "Enjoy the movie!" if not (or else), Python will print "Please bring a parent or guardian." Did You Know? With if-else statements, Python will always take some action. if(some condition): do this else: do that One or the other is going to happen. The if statement checks to see if something is true. If it's true, then the if action will occur. If it's false, then the else action will occur.


Related study sets

Mastering A&P (MAP) Chapter 4 -- tissues

View Set

MICRO: CH. 13 Monopolistic Competition Part I

View Set

FINN 3120 Chapter 11: Reading Questions

View Set