(E) flow control statements

Chapter # flow control statements (Video 39-57)
## Introduction to
Python code, when executed in the order down on the self-executing.
Through the process control statements may change the execution order of the program, but also allows repeated a plurality of times specified program
flow control statements divided into two categories: conditional statements, loop

## conditional statement (if statement)
Exercise 1:
Write a program, get a integer user input. Then displays this number is odd or even.
! # / usr / bin / Python
# - * - Coding: UTF-. 8 - * -
Number = int (the raw_input ( 'Enter an integer:'))
IF (Number% 2) ==. 1:
Print ( '% D is an odd number '% number)
the else:
Print ('% D is an even number '% number)

Exercise 2:
Write a program that checks whether any one year is a leap year.
If a year may be not divisible by 4 divisible by 100, or may be divisible by 400, this year is a leap year
# / usr / bin / Python!
# - * - Coding: UTF-. 8 - * -
Number = int (the raw_input ( 'please enter a year: '))
IF ((Number%. 4) == 0 && (Number 100%) = 0) || (Number 400%) == 0):!
Print ('% D is a leap '% number)
the else:
Print ( '% D is not a leap year'% number)

Exercise 3:
Our dog, 5 years old, 5-year-old dog is equivalent to how old people do?
In fact, very simple, two years before the dog is equivalent to 10.5 years of mankind every year, then every additional year increases the age of four.
Then 5-year-old dog is equivalent to the human age should be 10.5 + 10.5 + 4 + 4 + 4 = 33 years old

Write a program, get a dog's age entered by the user, then the program displays its human equivalent of age.
If the user enters a negative number, display a message
# / usr / bin / Python!
# - * - Coding: UTF-. 8 - * -
Number = int (the raw_input ( 'Enter a age:'))
IF (== Number . 1) || (Number == 2):
SUM = 10.5 * Number
elif (Number> 2):
SUM = (2 * 10.5) + ((Number - 2) *. 4)
Print ( '% D-year-old dog quite % d year-old human '% (number, sum))

 

## loop
Exercise 1:
seek within 100 all odd sum
! # / Usr / bin / Python
# - * - Coding: UTF-. 8 - * -
SUM = 0
for I in Range (100)
IF (I 2% ) ==. 1:
sUM = I +

Print ( '100 or less is the sum of all odd% d'% sum)

Exercise 2:
seek within 100 and all multiples of 7, and the number
! # / Usr / bin / Python
# - * - Coding: UTF-. 8 - * -
SUM = 0
COUNT = 0
for I in Range (100)
IF (I 7%) == 0:
sUM = I +
COUNT = +. 1

Print ( '100 or less and all are multiples of 7% d,% d total of a'% (sum, count))

Exercise 3:
number refers to a daffodils n bits (n ≧ 3), on which the digital bit of each n-th power sum equal to itself (for example: 3 + 1 * 3 + 3 * 5 ** * 3 = 153).
Seeking narcissistic number within all 1000
! # / Usr / bin / Python
# - * - Coding: UTF-. 8 - * -
SUM = 0
COUNT = 0
Number = 100
the while Number <1000:
A1 = 10% Number
A2 = Number / 10% 10
A3 = Number / 100% 10
SUM = (A1 + A2. 3 ** ** **. 3 + A3. 3)
IF == Number SUM:
Print (Number)
Number +. 1 =


## nested loop
Exercise 1:
print multiplication table 99
1 * 1 = 1
1 = 2 * 2 * 2 = 4 2
1 2 * 3 * 3 = 63 = 3 * 3 = 9
... 9 * 9 = 81
! # / usr / bin / Python
# - * - Coding: UTF-. 8 - * -
for I in Range (. 1, 10):
for J in Range (. 1, I +. 1):
Print (J, "*", I, "=", (I * J), End = "")
Print ()

Guess you like

Origin www.cnblogs.com/panzh/p/12516745.html