Python练习六

1.写函数,计算传入字符串中【数字】、【字母】、【空格】、以及【其他】的个数,并返回结果。

def day06_1(s):
    dic = {'num': 0, 'alpha': 0, 'space': 0, 'other': 0}
    for i in s:
        if i.isdigit():
            dic['num'] += 1
        elif i.isalpha():
            dic['alpha'] += 1
        elif i.isspace():
            dic['space'] += 1
        else:
            dic['other'] += 1
    return dic


print(day06_1('sdfjiosdf34430f=-=df'))

2.“一个程序读入三个整数。把此三个数值看成是一个三角形的三个边。这个程序要打印出信息,说明这个三角形是三边不等的、是等腰的、还是等边的。”

a = int(input('请输入一个整数:'))
b = int(input('请输入一个整数:'))
c = int(input('请输入一个整数:'))
if (a + b > c and a - b < c) or (a + c > b and a - c < b) or (b + c > a and b - c < a):
    if a == b and a == c and b == c:
        print("由{}、{}、{}组成的三角形是等边三角形".format(a, b, c))
    elif a == b or a == c or b == c:
        print("由{}、{}、{}组成的三角形是等腰三角形".format(a, b, c))
    else:
        print("由{}、{}、{}组成的三角形是三边不等的三角形".format(a, b, c))
else:
    print("由{}、{}、{}这三个数不能组成一个三角形".format(a, b, c))

3.假设商店货品价格(R)皆不大于100元(且为整数),若顾客付款在100元内 (P) ,求找给顾客最少货币个(张)数?(货币面值50元10 元,5 元,1元四 种 ).

price = int(input('请输入商品的价格:'))
money = int(input('请输入您给的钞票面值:'))
change = money - price
num = 0
while change >= 50:
    num += 1
    change -= 50
while change >= 10:
    num += 1
    change -= 10
while change >= 5:
    num += 1
    change -= 5
while change >= 1:
    num += 1
    change -= 1
print(num)

4.某城市电话号码由三部分组成。它们的名称和内容分别是:

(1)地区码:空白或三位数字;

(2)前 缀:非'0'或'1'的三位数字;

(3)后 缀:4位数字。

 假定被测程序能接受一切符合上述规定的电话号码,拒绝所有不符合规定的电话号码。

s = 0
phone = input('请输入电话号码:')
if phone.isdigit():
    if len(phone) == 10:
        for i in phone[3:6]:
            if i in ['0', '1']:
                print("你输入的电话号码不符合前缀为非0或非1的规则")
                break
            else:
                s += 1
                if s == 3:
                    print("你输入的电话号码符合规则")
    elif len(phone) == 7:
        for i in phone[0:3]:
            if i in ['0', '1']:
                print("你输入的电话号码不符合前缀为非0或非1的规则")
                break
            else:
                s += 1
                if s == 3:
                    print("你输入的电话号码符合规则")
    else:
        print("电话号码只能是10位数字或7位数字")
else:
    print('对不起,电话号码只能由数字组成')

5.程序有三个输入变量month、day、year(month 、 day和year均为整数值,并且满足:1≤month≤12和1≤day≤31),分别作为输入日期的月份、日、年份,通过程序可以输出该输入日期在日历上隔一天的日期。例如,输入为 2004 年11月29日,则该程序的输出为2004年12月1日。 

year = input('请输入年份:')
month = input('请输入月份:')
day = input('请输入日期:')
if year.isdigit() and month.isdigit() and day.isdigit():
    year = int(year)
    month = int(month)
    day = int(day)
    if month <= 12 and day <= 31:
        if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
            if month in [1, 3, 5, 7, 8, 10, 12]:
                if day + 2 <= 31:
                    print("{}年{}月{}日".format(year, month, day + 2))
                elif day + 2 == 32:  # 30
                    if month < 12:
                        print("{}年{}月{}日".format(year, month + 1, day - 29))
                    else:
                        print("{}年{}月{}日".format(year + 1, month - 11, day - 29))
                elif day + 2 == 33:  # 31
                    if month < 12:
                        print("{}年{}月{}日".format(year, month + 1, day - 29))
                    else:
                        print("{}年{}月{}日".format(year + 1, month - 11, day - 29))
            elif month == 2:
                if day + 2 <= 29:
                    print("{}年{}月{}日".format(year, month, day + 2))
                elif day + 2 == 30:  # 28
                    print("{}年{}月{}日".format(year, month + 1, day - 27))
                elif day + 2 == 31:  # 29
                    print("{}年{}月{}日".format(year, month + 1, day - 27))
            else:
                if day + 2 <= 30:  # 28
                    print("{}年{}月{}日".format(year, month, day + 2))
                elif day + 2 == 31:  # 29
                    print("{}年{}月{}日".format(year, month + 1, day - 28))
                elif day + 2 == 32:  # 30
                    print("{}年{}月{}日".format(year, month + 1, day - 28))

        else:
            if month in [1, 3, 5, 7, 8, 10, 12]:
                if day + 2 <= 31:
                    print("{}年{}月{}日".format(year, month, day + 2))
                elif day + 2 == 32:  # 30
                    if month < 12:
                        print("{}年{}月{}日".format(year, month + 1, day - 29))
                    else:
                        print("{}年{}月{}日".format(year + 1, month - 11, day - 29))
                elif day + 2 == 33:  # 31
                    if month < 12:
                        print("{}年{}月{}日".format(year, month + 1, day - 29))
                    else:
                        print("{}年{}月{}日".format(year + 1, month - 11, day - 29))
            elif month == 2:
                if day + 2 <= 28:
                    print("{}年{}月{}日".format(year, month, day + 2))
                elif day + 2 == 29:  # 27
                    print("{}年{}月{}日".format(year, month + 1, day - 26))
                elif day + 2 == 30:  # 28
                    print("{}年{}月{}日".format(year, month + 1, day - 26))
            else:
                if day + 2 <= 30:  # 28
                    print("{}年{}月{}日".format(year, month, day + 2))
                elif day + 2 == 31:  # 29
                    print("{}年{}月{}日".format(year, month + 1, day - 28))
                elif day + 2 == 32:  # 30
                    print("{}年{}月{}日".format(year, month + 1, day - 28))




    else:
        print("你输入的月份或年份不符合规则")
else:
    print("你输入的年份月份或日期不符合规则。 注:只能是数字")

猜你喜欢

转载自www.cnblogs.com/lin961234478/p/10537590.html