11、python基础学习-while循环

1、while 条件 :
print(“ ”)

2、输出1-10:
#!/usr/bin/env python

num = 1
while num <= 10 :
print(num)
num += 1

3、输出1-10之间的偶数:
#!/usr/bin/env python

num = 1
while num <= 10 :
if num % 2 == 0 :
print(num)
num += 1

4、猜年龄:
#!/usr/bin/env python

age = 36
flag = True
while flag:
user_input_age = int(input("your guess age>>>:"))
if user_input_age == age :
print("your are right!...")
flag = False
elif user_input_age > age :
print("Is bigger!...")
else :
print("Is smaller!...")
print("game over!...")

# d:\python-note>python python_test.py
# your guess age>>>:10
# Is smaller!...
# your guess age>>>:50
# your guess age>>>:36
# your are right!...
# game over!...

5、break猜年龄:
#!/usr/bin/env python

age = 36
# flag = True
while True:
user_input_age = int(input("your guess age>>>:"))
if user_input_age == age :
print("your are right!...")
#flag = False
break
elif user_input_age > age :
print("Is bigger!...")
else :
print("Is smaller!...")
print("game over!...")

# d:\python-note>python python_test.py
# your guess age>>>:10
# Is smaller!...
# your guess age>>>:50
# your guess age>>>:36
# your are right!...
# game over!...

6、continue:结束本次循环

#!/usr/bin/env python

print("hello word!",end="")
print("hello word!",end="")
print("hello word!",end="\n")
print("hello word!",end="")
end="" 不换行 , end="\n" 换行(默认值)

7、打印长方形:
#!/usr/bin/env python

hei = int(input("hei>>>:"))
wid = int(input("wid>>>:"))

num_hei = 1
while num_hei <= hei :
num_wid = 1
while num_wid <= wid :
print("#",end="")
num_wid += 1
print()
num_hei += 1

# d:\python-note>python python_test.py
hei>>>:3
wid>>>:5
#####
#####
#####

8、打印正三角:
#!/usr/bin/env python

wid = int(input("wid>>>:"))

num_hei = 0
while num_hei <= wid :
num_wid = 1
while num_wid <= num_hei :
print("#",end="")
num_wid += 1
print()
num_hei += 1

leng = int(input("请输入三角形的边长:"))
# 正三角形
i = 1
while i <= leng:
print("*" * i)
i += 1

# D:\python-note>python python_test.py
wid>>>:6

#
##
###
####
#####
######
请输入三角形的边长:6
*
**
***
****
*****
******

9、打印倒三角:
#!/usr/bin/env python

wid = int(input("wid>>>:"))

num_hei = 0
while num_hei <= wid :
num_wid = num_hei
while num_wid <= wid :
print("#",end="")
num_wid += 1
print()
num_hei += 1

# D:\python-note>python python_test.py
wid>>>:6
#######
######
#####
####
###
##
#


10、while打印九九乘法表:
#!/usr/bin/env python

a = 1
while a <= 9:
b = 1
while b <= a :
print(str(a)+"*"+str(b)+"="+str(a*b),end="\t") #不换行,打印制表符
b += 1
print() #换行,等同于print("\n")
a += 1

# D:\python-note>python python_test.py
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81

#!/usr/bin/env python

a = 9
while a >= 1:
b = 1
while b <= a :
print(str(a)+"*"+str(b)+"="+str(a*b),end="\t")
b += 1
print()
a -= 1

# D:\python-note>python python_test.py
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
4*1=4 4*2=8 4*3=12 4*4=16
3*1=3 3*2=6 3*3=9
2*1=2 2*2=4
1*1=1

上一篇:立体视觉—计算视差图


下一篇:mybatis批量插入