Python3 循环和判断小练习

设计一个函数, 在桌面上创建10个文本, 以数字给它们命名

def text_creation():
    path = r'C:UsersBlackDesktop	est\'
    for name in range(1, 11):
        with open(path + str(name) + '.txt', 'w', encoding='utf-8') as f:
            f.write(str(name))
            print('Done!')

text_creation()

设计一个复利计算函数 invest(), 它包含三个参数: amount(资金), rate(利率), time(投资时间). 输入每个参数后调用函数, 应该返回每一年的资金总额

def invest(amount, rate, time):
    for year in range(1, time + 1):
        amount = amount * (1 + rate)
        print(f'year {year} : ${amount}')
        
invest(100, 0.05, 10)

'''
year 1 : $105.0
year 2 : $110.25
year 3 : $115.7625
year 4 : $121.55062500000001
year 5 : $127.62815625000002
year 6 : $134.00956406250003
year 7 : $140.71004226562505
year 8 : $147.74554437890632
year 9 : $155.13282159785163
year 10 : $162.8894626777442
'''

摇骰子(3个), 猜大小. 点数小于10则为小, 大于10则为大

import random


def dice_game():
    while True:
        print('<<<< GAME STARTS! >>>>>')
        point1 = random.randrange(1, 7)
        point2 = random.randrange(1, 7)
        point3 = random.randrange(1, 7)

        lis = [point1, point2, point3]
        if sum(lis) <= 10:
            result = 'Small'
        else:
            result = 'Big'

        guess = input('Big or Small: ')

        if guess in ['Big', 'Small']:
            print('<<<< ROLL THE DICE! >>>>>')

            if guess == result:
                print(f'The point are {lis} You Win!')
            else:
                print(f'The point are {lis} You Lose!')
            break
        else:
            print('Invalid Words!')


dice_game()

在上一个项目的基础上增加下注功能, 赔率默认为1, 初始金额为1000, 当金额为0时退出游戏

import random


def dice_game():
    money = 1000
    while True:
        print('<<<< GAME STARTS! >>>>>')
        point1 = random.randrange(1, 7)
        point2 = random.randrange(1, 7)
        point3 = random.randrange(1, 7)

        lis = [point1, point2, point3]
        if sum(lis) <= 10:
            result = 'Small'
        else:
            result = 'Big'

        guess = input('Big or Small: ')
        bet = int(input('How much you wanna bet? - '))

        if money - bet < 0:
            print('余额不足!')
            continue

        if guess in ['Big', 'Small']:
            print('<<<< ROLL THE DICE! >>>>>')

            if guess == result:
                print(f'The point are {lis} You Win!')
                money += bet
                print(f'You gained {bet}, you have {money} now!')
            else:
                print(f'The point are {lis} You Lose!')
                money -= bet
                print(f'You lose {bet}, you have {money} now!')

        else:
            print('Invalid Words!')

        if money == 0:
            print('GAME OVER')
            break


dice_game()

给定各运营商号段, 判断用户输入号码的运营商, 要求如下:

  1. 号码长度不少于11位
  2. 输入的号码必须是数字
  3. 号码是运营商号段中的一个号码
def number_verification():
    CN_mobile = [134, 135, 136, 137, 138, 139, 150, 151, 152, 157, 158, 159, 182, 183, 184, 187, 188, 147, 178, 1705]
    CN_union = [130, 131, 132, 155, 156, 185, 186, 145, 176, 1709]
    CN_telecom = [133, 153, 180, 181, 189, 177, 1700]

    while True:
        number = input('Enter your number: ')

        if not number.isdigit():
            print('Invalid input, please enter digits')
            continue

        if not len(number) == 11:
            print('Invalid length, your number should be in 11 digits')
            continue

        first_three = int(number[0:3])
        first_four = int(number[0:4])

        if first_four in CN_mobile or first_three in CN_mobile:
            print('Operator: China mobile')
            print(f'We are sending verification code via text to your phone: {number}')
            break


        elif first_four in CN_union or first_three in CN_union:
            print('Operator: China union')
            print(f'We are sending verification code via text to your phone: {number}')
            break


        elif first_four in CN_telecom or first_three in CN_telecom:
            print('Operator: China telecom')
            print(f'We are sending verification code via text to your phone: {number}')
            break

        else:
            print('No such a operator!')


number_verification()


原文地址:https://www.cnblogs.com/bigb/p/11638057.html