1, if statement
Syntax:
if condition:
indented statement(s)
If the condition is true, execute these indented statements, otherwise skip these statements
example:
answer = input("Do you want to see a spiral? y/n:") if answer == 'y': print("Working...") import turtle t = turtle.Pen() t.width(2) for x in range(100): t.forward(x*2) t.left(89) print("Okay, we're done!")
If you enter "y", draw the helix, otherwise directly output "Okay, we're done! “
2, Boolean expressions and Boolean values
Boolean expression, or conditional expression, the result is Boolean, either True or False, and the first letter should be capitalized.
Syntax: expression1 conditional_operator expression2
2.1. Comparison operator
operator | describe | example |
---|---|---|
== | Equal - whether the comparison objects are equal | (a == b) returns False. |
!= | Not equal - compares whether two objects are not equal | (a! = b) returns True. |
> | Greater than - Returns whether x is greater than y | (a > b) returns False. |
< | Less than - Returns whether x is less than y. all comparison operators return 1 for True and 0 for False. This is equivalent to the special variables True and False, respectively. Note that these variable names are capitalized. | (a < b) returns True. |
>= | Greater than or equal - Returns whether x is greater than or equal to y. | (a > = b) returns False. |
<= | Less than or equal to - Returns whether x is less than or equal to y. | (a < = b) returns True. |
2.2. You are not big enough
# OldEnough.py driving_age = eval(input("What is the legal driving age where you live? ")) your_age = eval(input("How old are you? ")) if your_age >= driving_age: print("You're old enough to drive!") if your_age < driving_age: print("Sorry, you can drive in", driving_age - your_age, "years.")
After the result of the first conditional expression is known, the result of the second conditional expression must also be known and does not need to be calculated again, so the final if can be replaced by else, which is more concise.
3, else statement
Syntax:
if condition:
indented statement(s)
else:
other indented statement(s)
If the conditional expression is True, the indented statement is executed; otherwise, other indented statements are executed
Modify the above example:
# OldEnoughOrElse.py driving_age = eval(input("What is the legal driving age where you live? ")) your_age = eval(input("How old are you? ")) if your_age >= driving_age: print("You're old enough to drive!") else: print("Sorry, you can drive in", driving_age - your_age, "years.")
3.1. Polygon or rose petal
# PolygonOrRosette.py import turtle t = turtle.Pen() # Ask the user for the number of sides or circles, default to 6 number = int(turtle.numinput("Number of sides or circles", "How many sides or circles in your shape?", 6)) # Ask the user whether they want a polygon or rosette shape = turtle.textinput("Which shape do you want?", "Enter 'p' for polygon or 'r' for rosette:") for x in range(number): if shape == 'r': # User selected rosette t.circle(100) else: # Default to polygon t.forward (150) t.left(360/number)
3.2. Even or odd
# RosettesAndPolygons.py - a spiral of polygons AND rosettes! import turtle t = turtle.Pen() # Ask the user for the number of sides, default to 4 sides = int(turtle.numinput("Number of sides", "How many sides in your spiral?", 4)) # Our outer spiral loop for polygons and rosettes, from size 5 to 75 for m in range(5,75): t.left(360/sides + 5) t.width(m//25+1) t.penup() # Don't draw lines on spiral t.forward(m*4) # Move to next corner t.pendown() # Get ready to draw # Draw a little rosette at each EVEN corner of the spiral if (m % 2 == 0): for n in range(sides): t.circle(m/3) t.right(360/sides) # OR, draw a little polygon at each ODD corner of the spiral else: for n in range(sides): t.forward(m) t.right(360/sides)
4, elif statement
# WhatsMyGrade.py grade = eval(input("Enter your number grade (0-100): ")) if grade >= 90: print("You got an A! :) ") elif grade >= 80: print("You got a B!") elif grade >= 70: print("You got a C.") elif grade >= 60: print("You got a D...") else: print("You got an F. :( ")
5, Complex conditions ---- if, and, or, and not
The following variables are assumed to be 10 for a and 20 for B:
operator | Logical expression | describe | example |
---|---|---|---|
and | x and y | Boolean and - if x is False, x and y return the value of X, otherwise return the calculated value of y. | (a and b) return to 20. |
or | x or y | Boolean or - if x is True, it returns the value of X, otherwise it returns the calculated value of y. | (a or b) return to 10. |
not | not x | Boolean not - Returns False if x is True. It returns True if x is False. | not(a and b) returns False |
# WhatToWear.py rainy = input("How's the weather? Is it raining? (y/n)").lower() cold = input("Is it cold outside? (y/n)").lower() if (rainy == 'y' and cold == 'y'): # Rainy and cold, yuck! print("You'd better wear a raincoat.") elif (rainy == 'y' and cold != 'y'): # Rainy, but warm print("Carry an umbrella with you.") elif (rainy != 'y' and cold == 'y'): # Dry, but cold print("Put on a jacket, it's cold out!") elif (rainy != 'y' and cold != 'y'): # Warm and sunny, yay! print("Wear whatever you want, it's beautiful outside!")
6, Secret information
message = input("Enter a message to encode or decode: ") # Get a message message = message.upper() # Make it all UPPERCASE :) output = "" # Create an empty string to hold output for letter in message: # Loop through each letter of the message if letter.isupper(): # If the letter is in the alphabet (A-Z), value = ord(letter) + 13 # shift the letter value up by 13, letter = chr(value) # turn the value back into a letter, if not letter.isupper(): # and check to see if we shifted too far value -= 26 # If we did, wrap it back around Z->A letter = chr(value) # by subtracting 26 from the letter value output += letter # Add the letter to our output string print("Output message: ", output) # Output our coded/decoded message
task
1. Colored rose petals and spirals
Modify # RosettesAndPolygons.py to achieve the following effects: