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

Python--while循环

时间:2019-12-26 17:51:03      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:+=   输出   big   input   pre   翻转   put   练习   int   

while循环:

num = 1

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

限定num = 1,100以内取偶数:

num = 1

while num<=100:
    if num%2 == 0: #整除2,余数为0
        print(num)
    num += 1 

猜数字游戏:

my_of_age = 25
guess_age = True
while guess_age :
    guess_age = int(input("my age is:"))
    if guess_age == my_of_age:
        print("yes,you are right")
        guess_age = False
    elif guess_age>my_of_age:
        print("Is bigger")
    elif guess_age<my_of_age:
        print("Is smaller")

嵌套循环:

width = int(input("width:"))
higth = int(input("higth:"))

#gao = higth 此处没有必要重新命名
while higth>=1 :
    
    kuan = width
    while kuan>=1 :
        print("#",end="")
        kuan -= 1
    print()    #换行的作用

    higth  -= 1

练习题:

如何输出一个如下的直角三角形,用户指定输出行数:(如果上下翻转,又如何输出?)

****

***

**

*

width = int(input("width:"))

while width > 0 :
    
    line = width
    while line > 0 :
        print("*",end="")
        line -= 1
    print()
    
    width -= 1

99乘法表:

倒着的:

chang = 9

while chang>0 :

    line = 1
    while line<=chang :
        print(str(line)+"*"+str(chang)+"=",line*chang,end=" ")
        line +=1
    print()   
    chang -=1

正向的:

chang = 1
while chang<=9 :

    line = 1
    while line<=chang :
        print(str(line)+"*"+str(chang)+"=",line*chang,end=" ")
        line +=1
        
    print()   
    chang +=1

Python--while循环

标签:+=   输出   big   input   pre   翻转   put   练习   int   

原文地址:https://www.cnblogs.com/tingwen/p/12060645.html

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