码迷,mamicode.com
首页 > 编程语言 > 详细

11、python基础学习-while循环

时间:2019-05-25 15:40:10      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:while   ***   while循环   inpu   print   5*   三角形   循环   guess   

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

11、python基础学习-while循环

标签:while   ***   while循环   inpu   print   5*   三角形   循环   guess   

原文地址:https://www.cnblogs.com/hlc-123/p/10922563.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!