Python 学习2

Python-if,while, for循环

key points

if语句,及if语句套用

  1. if…else
1
2
3
4
5
6
7
8
name=input()  #显示输入框,name=输入内容 show the inputbox,name=input #input 函数
if name == 'Mary': #if 语句
print('Hello Mary')
password=input()
if password == 'swordfish':
print('Access granted.')
else: #if...else语句
print('Wrong password.')

该代码可判断用户名及密码是否与设定值相符

check the user‘s name and password

  1. if…elif
1
2
3
4
5
6
7
8
9
10
11
12
name=input()
if name == 'Alice':
print('Hi,Alice.')
else:
print('What is your age?')
age=input()
if age < '12':
print('You are not Alice, kiddo.')
elif age > '100':
print('You are not Alice,grannie.')
elif age > '2000':
print('Unlike you, Alice is not an undead, immortal vampire.')

该代码通过判断年龄确定身份

check the user through age

while语句

1
2
3
4
spam = 0
while spam < 5:
print('Hello world!')
spam = spam+1

output:

1
2
3
4
5
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!

‘break’ and ‘continue’

break:

When the break is executed, the loop ends.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
spam=0
while True:
print('Hello world')
if spam==3:
break
spam=spam+1
print(spam)

#out:
#Hello world
#Hello world
#Hello world
#Hello world
#3

continue:

When continue is executed, the loop starts again.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
spam=1
while spam<4:
print('Hello!')
spam=spam+1
if spam==3:
continue
print('Hello world')
print(' ')
print(spam)

#out:
#Hello!
#Hello world
#
#Hello! during this time, spam=3, so it didn't print 'Hello world'.
#Hello!
#Hello world
#4

while+if

输入密码 input the password

1
2
3
4
5
6
7
8
9
10
key=input() 
spam=0 #record the times tried, make sure no more than 3 times 不超过三次
if int(key)==12345:
print('Pass')
while spam < 2 and int(key)!=12345:
print('Try again')
key=input()
spam=spam+1 #计次 record thetimes
if int(key)==12345 and spam!=0:
print('Pass')

e.g.

1
2
3
4
5
6
>1
Try again
>12
Try again
>12345
Pass

another way:

1
2
3
4
5
6
7
8
9
10
11
12
13
key=input()
if key=='12345':
print('pass')
else:
print('try again')
key=input()
if key=='12345':
print('pass')
else:
print('try again')
key=input()
if key=='12345':
print('pass')

for循环

1
2
for i in range(12,16):
print(i)

12到16的所有整数

print all integers in 12 to 16

1
2
3
for i in range(0,10,2): #the 2 here means the step is 2
print(i)
#out:0 2 4 6 8
1
2
3
for i in range(5,-1,-1): 
print(i)
#output:5 4 3 2 1 0

example

a tricky game:

You must input ‘your name’ instead of the real name:

你要输入 ”your name” 而不是真的名字

1
2
3
4
5
6
7
print('Please type your name.')
yourname=input()
while yourname!='your name':
print('Please type your name.')
yourname=input()
if yourname=='your name':
print('Thank you!')

e.g.

1
2
3
4
5
6
7
Please type your name.
Alice
Please type your name.
Emma
Please type your name.
your name
Thank you!

another way:

1
2
3
4
5
6
while True:
print('Please type your name.')
name=input()
if name == 'your name':
break
print('Thank you!')

make patterns!

1

1
2
3
4
5
6
a=6
while True:
print('*'*a) #'*'is the string while * is 'x', multiply
a=a-1 #the number of a is decreasing
if a==0:
break #if there is no more '*', it ends

e.g.

1
2
3
4
5
6
******
*****
****
***
**
*

2

1
2
3
4
5
6
7
8
9
10
11
a=1
while True:
print('*'*a)
a=a+1
if a==6:
break
while True:
print('*'*a)
a=a-1
if a==0:
break
1
2
3
4
5
6
7
8
9
10
11
*
**
***
****
*****
******
*****
****
***
**
*

another way

1
2
3
4
for a in range(1,7):
print('*'*a)
for a in range(5,0,-1):
print('*'*a)

calculation of factorial

阶乘计算

1
2
3
4
5
result=1
a=int(input())
for i in range(1,a+1):
result=result*i
print(result)

e.g.

input=9

output=362880