Python 作业

  • 设计"过7游戏"的程序,打印出1-100之间除了含7和7的倍数之外的所有数字

    i = 0
    while i <= 99:
        i += 1
        if i % 7 == 0:
            continue
        elif i - i // 10 * 10 == 7:
            continue
        elif i//10 == 7:
            continue
        print(i, end=" ")
        
    1 2 3 4 5 6 8 9 10 11 12 13 15 16 18 19 20 22 23 24 25 26 29 30 31 32 33 34 36 38 39 40 41 43 44 45 46 48 50 51 52 53 54 55 58 59 60 61 62 64 65 66 68 69 80 81 82 83 85 86 88 89 90 92 93 94 95 96 99 100 
    
  • 编写程序用户登录程序仅有三次机会。(if和for)

    flag = 0
    for i in range(3):
        username = input('请输入用户名:')
        password = input('请输入密码:')
        if username == '123' and password == '456':
            flag = 1
            print('登录成功!')
            break
    if flag == 0:
        print('三次输入错误,退出程序')
        
        
    请输入用户名:123
    请输入密码:456
    登录成功!
    
  • 编写程序实现,判断一串字符串是否为有效变量名

    import string
    flag = 0
    str = input("请输入变量名:")
    a = string.ascii_letters
    b = string.digits
    if str[0] in a or str[0] == '_':
        for i in str[1:]:
            if i not in a and i not in b and i !='_':
                flag = 1
    
        if flag == 1:
            print("变量名错误")
        else:
            print("变量名正确")
    else:
        print("变量名错误")
    
  • 最多猜10次数字的游戏,猜测范围1~100,根据input内容提示猜大或者猜小,如果猜中,结束循环

    a = 58
    for i in range(10):
        num = int(input("请输入1-100中一个数:"))
        if num >= 1 and num <= 100:
            if num > a:
                print("偏大")
            elif num < a:
                print("偏小")
            else:
                print("猜中")
                break
        else:
            print("请输入正确范围的数")
    
  • 使用while循环实现输出2-3+4-5+6…+100的和

    i = 1
    a = 1
    sum = 0
    while i <= 100:
        sum += i * a
        i += 1
        a = -a
    print(sum)
    
    
    -50
    
  • 使用循环实现九九乘法表

    i = 1
    j = 1
    for i in range(1, 10):
        for j in range(1, i + 1):
            print(i, '*', j, '=', i * j, end=" ")
        print('')
        
    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 
    
    
  • 已知列表list=[22478,24066,23398,38498],利用字符串拼接及遍历,输出结果"城市学院"

a = ""
li = [22478, 24066, 23398, 38498]
for i in range(4):
    a += chr(li[i])
print(a)

城市学院

你可能感兴趣的:(Python课程)