100个python小示例

示例1.多条件判断

#出题 height
#女生找对象
    # 男生在1米~1.5米之间 小强 你在哪里?
    # 男生在1.5~1.7米之间 没有安全感~
    # 男生 1.7~ 1.8米之间 帅哥 留个电话
    # 男生 1.8~2米之间 帅哥 你建议多一个女朋友吗

while True:
    print('>>>> 按q退出!')
    height = (input('请输入你的身高: '))
    if height.lower() == 'q':
        break
    height = float(height)
    if 1 <= height < 1.5:
        print('小强,你在哪里?')
    elif 1.5 <= height < 1.7:
        print('没有安全感~')
    elif 1.7 <= height < 1.8:
        print('帅哥,留个电话')
    elif 1.8 <= height < 2:
        print('帅哥,你建议多一个女朋友吗?')
    else:
        print('抱歉,你的身高不在范围内!')

示例2. while 循环

2.通过打印一个变量的形式,展现一行十个小星星?
i = 1
strvar = ""
while i <=10:
	strvar += "☆"
	i += 1
print(strvar)
	
3.一行十个换色的星星?
i = 1
while i <=10:
    if i % 2 == 0:
        print('-',end="")
    else:
        print('+',end="")
    i += 1
4.用一个循环打印十行十列小星星?
#方法一
# i = 0
# while i<100:
#     print('*',end="")
#     if i % 10 ==9:
#         print()
#     i += 1

#方法二
i = 1
while i<=100:
    print('*',end="")
    if i % 10 ==0:
        print()
    i += 1
5.一个循环实现十行十列隔列换色的小星星?
i = 0
while i <100:
    if i % 2 == 0:
        print('-',end="")
    else:
        print('+',end="")
    if i % 10 == 9:
        print()
    i += 1

6.一个循环实现十行十列,隔行换色小星星?
i = 0
while i < 100:
    if i // 10 % 2== 0:
        print('-',end="")
    else:
        print('+',end="")
    if i % 10 == 9:
        print()
    i += 1

示例3. 输入次数计数

times = 1
while times <= 3:
    pwd = input("请输入您的密码:")
    if pwd == "111":
        print("恭喜你登录成功~")
        break

    # 剩余次数 = 总次数 - 已经使用的次数
    print("你剩余的输入次数是%d" % (3 - times))
    times += 1

示例4. while双循环

# 打印10行10列的小星星
j = 0 while j < 10: i = 0 while i < 10: print('*', end='') i += 1
#每打印10个小星星就换行 print() j += 1 ''' 里层的i比外层的j要循环的快,外层的j循环1次,里层的i循环10次,外层循环2次,里层20次...,依次类推 最后10*10次就是100个小星星 '''

10行10列,隔列换色的小星星

i = 0
while i < 10:
    j = 0
    while j < 10:
        if j % 2 == 0:
            print('+',end="")
        else:
            print('-',end='')
        j += 1
    print()
    i += 1

10行10列,隔行换色的小星星

i = 0
while i < 10 :
    j = 0
    while j < 10:
        if i % 2 == 0:
            print("",end="")
        else:
            print("",end="")
            
        j +=1
    print()
    i +=1

示例5. 99乘法表

i = 1
while i <= 9:
    
    # 打印对应的表达式
    j = 1
    while j <= i:
        print("%d*%d=%2d " % (i,j,i*j) ,end="" )
        j+=1
    
    # 打印换行
    print()
    
    i +=1

方法2. for循环99乘法表

for i in range(1,10):
    for j in range(1,i+1):
        print("%d*%d=%2d " % (i,j,i*j) ,end="" )
    print()

示例6: 百钱买百鸡

# 百钱买百鸡
# 公鸡一个五块钱,母鸡一个三块钱,小鸡三个一块钱,现在要用一百块钱买一百只鸡,问公鸡、母鸡、小鸡各多少只?
"""
穷举法:把数据拿出来一个一个试
x = [1,2]
y = [3,4]
z = [5,6]
x+y+z = 10
1 + 3 + 5 = 9
1 + 3 + 6 = 10 bingo
1 + 4 + 5 = 10 bingo
1 + 4 + 6 = 11
2 + 3 + 5 = 10 bingo
2 + 3 + 6 = 11
2 + 4 + 5 = 11
2 + 4 + 6 = 12
"""

"""
公鸡 : x  母鸡 : y  小鸡: z
鸡的数量:x + y + z = 100
鸡的价格:5 * x + 3 * y + 1/3*z = 100   
"""

x = 0
while x <= 20:
    
    y = 0
    while y <= 33:
        
        z = 0
        while z <= 100:
            
            if x+y+z == 100 and 5*x + 3 * y + 1/3*z == 100 :
                print(x,y,z)
            z += 1    
        
        y +=1
    
    x += 1

示例7 求吉利数字

# 求吉利数字 100 ~ 999 之间 找 111 222 333 123 456 654 321 ... 
"""
// 可以获取一个数高位
%  可以获取一个数低位
baiwei = 345 // 100
shiwei = 345 // 10 % 10
gewei  = 345 % 10
print(gewei)
"""

# 方法一
i = 100
while i <= 999:
    baiwei = i // 100
    shiwei = i // 10 % 10
    gewei = i % 10

    if shiwei == gewei  and shiwei == baiwei :
        print(i)
    # 123
    elif shiwei == gewei - 1 and shiwei == baiwei + 1:
        print(i)
    # 987
    elif shiwei == gewei + 1 and shiwei == baiwei - 1:
        print(i)
    i +=1

# 方法二
print("<====>")
i = 100
while i <= 999:
    strvar = str(i)
    # print(strvar, type(strvar))
    gewei = int(strvar[-1])
    shiwei = int(strvar[1])
    baiwei = int(strvar[0])
    if shiwei == gewei  and shiwei == baiwei :
        print(i)
    # 123
    elif shiwei == gewei - 1 and shiwei == baiwei + 1:
        print(i)
    # 987
    elif shiwei == gewei + 1 and shiwei == baiwei - 1:
        print(i)
    
    i +=1

# 方法三
print("<====>")
i = 100
while i <= 999:
    strvar = str(i)
    # print(strvar, type(strvar))
    gewei = int(strvar[-1])
    shiwei = int(strvar[1])
    baiwei = int(strvar[0])

    if 2 * shiwei == gewei + baiwei and (shiwei == gewei + 1 or shiwei == gewei -1 ):
        print(i)
    elif gewei == shiwei and shiwei == baiwei:
        print(i)
    
    i +=1

 示例8  国际棋盘

# 一个循环打印2行,循环4次就是8行
i = 0
while i < 4:
strvar = ""
j = 0
while j < 8:
if j % 2 == 0:
strvar += "□"
else:
strvar += "■"
j += 1
print(strvar) # 正向打印白黑
print(strvar[::-1]) # 反向打印黑白
i += 1

 

 

 

 

 

posted @ 2022-01-12 16:25  urls  阅读(752)  评论(0编辑  收藏  举报