码迷,mamicode.com
首页 > 其他好文 > 详细

while循环实例

时间:2018-06-15 16:07:22      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:练习   int   class   bre   使用   user   idt   input   print   

输出1到100之间的偶数:

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

猜年龄:

age = 50

#user_input_age = int(input("Age is:"))
flag = True

while flag:
    user_input_age = int(input("Age is:"))
    if user_input_age == age:
        print("Yes")
        flag = False
    elif user_input_age > age:
        print("Is bigger.")
    else:
        print("Is smaller")
        
print("End")

continue statement:

num = 1
while num <= 10:
    num += 1
    if num == 3: 
        continue
    print(num)
else:
    print("This is else statement")

break statement:

num = 1
while num <= 10:
    num += 1
    if num == 5: 
        break
    print(num)
else:
    print("This is else statement")

三行转换成一行:

print("Hello world", end="")
print("Hello world", end="")
print("Hello world", end="")

while语句嵌套:

num1 = 0

while num1 <= 5:
    print(num1, end = "_")
    num2 = 0
    while num2 <= 7:
        print(num2, end = "-")
        num2 += 1
    
    num1 += 1
    print()

while语句嵌套小练习:使用#输出长方形,用户可以定义长度和宽度

height = int(input("Height:"))
width = int(input("Width:"))
num_height = 1
while num_height <= height:
    num_width = 1
    while num_width <= width:
        print("#", end = "")
        num_width += 1
    print()
    num_height +=1

 

while循环实例

标签:练习   int   class   bre   使用   user   idt   input   print   

原文地址:https://www.cnblogs.com/evatan123/p/9186985.html

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