万事开头难

双项循环经典题

一. 双项循环经典练习

1.1用两个循环完成十行十列的小星星

j = 0
while j < 10:
    # 看成整体,一行十个星星
    i = 0
    while i < 10:
            print('*',end='')
            i+=1

    # 打印换行
    print()
    i+=1
View Code

1.2用两个循环完成十行十列隔列换色的星星

i = 0
while i < 10:
    # 打印一行 ★☆★☆★☆★☆★☆
    j = 0
    while j < 10:
        if j % 2 == 0:
            print('*',end='')
        else:
            print('#',end='')
        j+=1

    #打印换行
    print()
    i += 1
View Code

1.3用两个循环完成十行十列隔行换色的小星星

i = 0
while i < 10 :
    
    # 打印一行★☆★☆★☆★☆★☆
    j = 0
    while j < 10:
        if i % 2 == 0:
            print("",end="")
        else:
            print("",end="")
        j += 1
    # 打印换行
    print()
    i += 1
View Code

1.4 99乘法表

1.1方向一(正序)

i = 1
while i <= 9:
    j = 1
    while j <= i:
        print('%d*%d=%2d' % (i,j,i*j),end='')
        j+=1
    
    # 打印换行
    print()
    i+=1
View Code

1.2 方向二(倒序)

i = 9
while i >= 1:
    # 输出乘法表表达式
    j = 1
    while j <= i :
        print("%d*%d=%2d " % (i,j,i*j) ,end="")
        # print(i,j)
        j+=1
        
    # 打印换行
    print()
    
View Code

1.3 方向三(正序)

i = 1
while i <= 9 :
    # 1.先打印空格
    k = 9 - i 
    while k > 0 :
        print("       " ,  end="")
        k -= 1
        
    # 2.在打印表达式 
    j = 1
    while j <= i :
        print("%d*%d=%2d " % (i,j,i*j) ,end="")
        # print(i,j)
        j+=1
        
    # 3.在打印换行
    print()

    i += 1
View Code

 1.4 方向四(倒序)

i = 9
while i >= 1:

    # 1.先打印空格
    k = 9 - i 
    while k > 0 :
        print("       " ,  end="")
        k -= 1
        
    # 2.在打印表达式 
    j = 1
    while j <= i :
        print("%d*%d=%2d " % (i,j,i*j) ,end="")
        # print(i,j)
        j+=1
        
    # 3.在打印换行
    print()

    i -= 1
View Code

1.5  求吉利数字 100 ~ 999 之间 找 111 222 333 123 456 654 321 ...

方法一:

i = 100
while i < 1000:
    # 找百位
    baiwei = i // 100
    # 找十位
    shiwei = i // 10 % 10
    # 找个位 
    gewei = i % 10
    
    # 找 111 222 333 ... 
    if shiwei == baiwei and shiwei == gewei:
        print(i)    
    
    # 百位:1  十位:2  个位:3 # 123 456 789
    elif (shiwei == baiwei+1) and (shiwei == gewei - 1):
        print(i)
    
    # 百位:9 十位:8 个位:7   # 987
    elif (shiwei == baiwei - 1) and  (shiwei == gewei + 1 ):
        print(i)
    # print(baiwei,shiwei,gewei)
    i += 1
View Code

方法二:

i = 100
while i<=999:
    num = str(i)
    # print(num , type(num))
    baiwei = int(num[0])
    shiwei = int(num[1])
    gewei = int(num[-1])

    # 找 111 222 333 ... 
    if shiwei == baiwei and shiwei == gewei:
        print(i)    
    
    # 百位:1  十位:2  个位:3 # 123 456 789
    elif (shiwei == baiwei+1) and (shiwei == gewei - 1):
        print(i)
    
    # 百位:9 十位:8 个位:7   # 987
    elif (shiwei == baiwei - 1) and  (shiwei == gewei + 1 ):
        print(i)
    i += 1
View Code

1.6 百钱买百鸡

公鸡一个五块钱,母鸡一个三块钱,小鸡三个一块钱,现在要用一百块钱买一百只鸡,问公鸡、母鸡、小鸡各多少只?

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
View Code

1.7利用if语句写出猜大小的游戏

"""

设定一个理想数字比如:66,
让用户三次机会猜数字,如果比66大,则显示猜测的结果大了;
如果比66小,则显示猜测的结果小了;
只有等于66,显示猜测结果正确,退出循环。
最多三次都没有猜测正确,退出循环,并显示‘都没猜对,继续努力’。

"""

p = 3
a = True
while a:
    if p == 0:
        print('没有机会了')
        break
    intvar = int(input('请输入一个数字:'))
    if intvar == 66:
        print('正确')
    elif intvar < 66:
        p -= 1
        print('太小了还剩下%d次机会' % (p))
    elif intvar > 66:
        p -= 1
        print('太大了还剩下%d次机会' % (p))
    else:
        print('格式错误,输入的是数字')
View Code

1.8 使用while和for 遍历字符串 "IG战队牛逼"

for :
    a = 'TG战队nb'
    for i in a:
        print(i)

while:
    a = 'IG战队nb'
    i = 0
    while i < len(a):
        print(a[i])
        i+=1
View Code

1.9 使用for循环对s="黄绿青蓝紫"进行循环,每次打印的内容是每个字符加上"色的"

s="黄绿青蓝紫"
for i in s:
    print(i+'色的')
View Code

2.0 完成要求:

"""
用户可持续输入(while循环)
输入A,则显示走大路回家,然后在让用户进一步选择:
是选择公交车,还是步行?
选择公交车,显示10分钟到家,并退出整个程序。
选择步行,显示20分钟到家,并退出整个程序。
输入B,
则显示走小路回家,并退出整个程序。
输入C,
则显示绕道回家,然后在让用户进一步选择:
是选择游戏厅玩会,还是网吧?
选择游戏厅,则显示 ‘一个半小时到家,爸爸在家,拿棍等你。’并让其重新输入A,B,C选项。
选择网吧,则显示‘两个小时到家,妈妈已做好了战斗准备。’并让其重新输入A,B,C选项。
"""

a = True
while a :
    name = input('请输入回家方式:')
    if name == 'A':
        print('走大路回家')
        b = input('公交车还是步行:')
        if b == '公交车':
            print('10分钟到家')
        else:
            print('20分钟到家')
        a = False
    elif name == 'B':
        print('走小路回家')
        break
    elif name == 'C':
        print('绕道回家:')
        c = input('游戏厅玩会,还是网吧?')
        if c == '游戏厅':
            print('个半小时到家,爸爸在家,拿棍等你')
            continue
        else:
            print('两个小时到家,妈妈已做好了战斗准备')
            continue
View Code

2.1 计算 1 - 2 + 3 - 4 + ... + 99 中除了88以外所有数的总和

count = 0
for i in range(1, 100):
    if i == 88:
        continue
    elif i % 2 == 1:
        count += i
    else:
        count -= i
print(count)
View Code

2.2 (升级题)打印菱形小星星

 

for i in range(1,7):
    for k in range(6-i):
        print(' ',end='')
    for j in range(2*i-1):
        print('*',end='')
    print()
for i in range(6,0,-1):
    for j in range(1, 7 - i):
        print(' ', end='')
    for j in range(1, 2 * i):
        print('*', end='')
    print()
View Code

 

posted @ 2020-11-29 15:38  Bo7-w  阅读(177)  评论(0编辑  收藏  举报