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) and (a + c > b and a - c < b) and (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 ['', '']:
print("你输入的电话号码不符合前缀为非0或非1的规则")
break
else:
s += 1
if s == 3:
print("你输入的电话号码符合规则")
elif len(phone) == 7:
for i in phone[0:3]:
if i in ['', '']:
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: #
if month < 12:
print("{}年{}月{}日".format(year, month + 1, day - 29))
else:
print("{}年{}月{}日".format(year + 1, month - 11, day - 29))
elif day + 2 == 33: #
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: #
print("{}年{}月{}日".format(year, month + 1, day - 27))
elif day + 2 == 31: #
print("{}年{}月{}日".format(year, month + 1, day - 27))
else:
if day + 2 <= 30: #
print("{}年{}月{}日".format(year, month, day + 2))
elif day + 2 == 31: #
print("{}年{}月{}日".format(year, month + 1, day - 28))
elif day + 2 == 32: #
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: #
if month < 12:
print("{}年{}月{}日".format(year, month + 1, day - 29))
else:
print("{}年{}月{}日".format(year + 1, month - 11, day - 29))
elif day + 2 == 33: #
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: #
print("{}年{}月{}日".format(year, month + 1, day - 26))
elif day + 2 == 30: #
print("{}年{}月{}日".format(year, month + 1, day - 26))
else:
if day + 2 <= 30: #
print("{}年{}月{}日".format(year, month, day + 2))
elif day + 2 == 31: #
print("{}年{}月{}日".format(year, month + 1, day - 28))
elif day + 2 == 32: #
print("{}年{}月{}日".format(year, month + 1, day - 28))
else:
print("你输入的月份或年份不符合规则")
else:
print("你输入的年份月份或日期不符合规则。 注:只能是数字")
Python练习六的更多相关文章
- 简学Python第六章__class面向对象编程与异常处理
Python第六章__class面向对象编程与异常处理 欢迎加入Linux_Python学习群 群号:478616847 目录: 面向对象的程序设计 类和对象 封装 继承与派生 多态与多态性 特性p ...
- 初学Python(六)——输入输出
初学Python(六)——输入输出 初学Python,主要整理一些学习到的知识点,这次是输入输出. 输入: # -*- coding:utf-8 -*- ''''' python中的输出为print ...
- 孤荷凌寒自学python第六十九天学习并实践beautifulsoup对象用法2
孤荷凌寒自学python第六十九天学习并实践beautifulsoup对象用法2 (完整学习过程屏幕记录视频地址在文末) 今天继续学习beautifulsoup对象的属性与方法等内容. 一.今天进一步 ...
- 孤荷凌寒自学python第六十八天学习并实践beautifulsoup模块1
孤荷凌寒自学python第六十八天学习并实践beautifulsoup模块1 (完整学习过程屏幕记录视频地址在文末) 感觉用requests获取到网页的html源代码后,更重要的工作其实是分析得到的内 ...
- 孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块
孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块 (完整学习过程屏幕记录视频地址在文末) 从今天起开始正式学习Python的爬虫. 今天已经初步了解了两个主要的模块: ...
- python练习六十三:文件处理,读取文件内容,按内容生成文件
python练习六十三:文件处理 假设要读取code.txt文件中内容,code.txt文件内容如下 01 CN Chinese 02 US United States of America 03 J ...
- python练习六十一:文件处理,读取文件内容
python练习六十一:文件处理,读取文件内容 假设要读取text.txt文件中内容 写文件(如果有文件,那直接调用就行,我这里自己先创建的文件) list1 = ['python','jave',' ...
- 孤荷凌寒自学python第六十六天学习mongoDB的基本操作并进行简单封装5
孤荷凌寒自学python第六十六天学习mongoDB的基本操作并进行简单封装5并学习权限设置 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第十二天. 今天继续学习mongo ...
- 孤荷凌寒自学python第六十五天学习mongoDB的基本操作并进行简单封装4
孤荷凌寒自学python第六十五天学习mongoDB的基本操作并进行简单封装4 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第十一天. 今天继续学习mongoDB的简单操作 ...
- 孤荷凌寒自学python第六十四天学习mongoDB的基本操作并进行简单封装3
孤荷凌寒自学python第六十四天学习mongoDB的基本操作并进行简单封装3 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第十天. 今天继续学习mongoDB的简单操作, ...
随机推荐
- java 读取文件乱码
private void filereader() throws IOException{ BufferedReader bf= new BufferedReader(new InputStream ...
- PAT A1103
PAT A1103 标签(空格分隔): PAT 解题思路: DFS #include <cstdio> #include <vector> using namespace st ...
- windows2012的服务器远程桌面提示内部错误的问题解决方法
一.问题表象 我们在OpenStack安装了windows server2012r2版本的虚拟机,在本地通过远程桌面连接时,输入账号密码后,提示连接断开或者内部错误的问题 二.解决办法 1)windo ...
- org.springframework.cloud FeignInterceptor
package org.rx.feign; import org.apache.commons.lang3.ArrayUtils; import org.aspectj.lang.Proceeding ...
- 分布式session个人理解浅谈
在分布式中,用户的session如何处理呢? 服务器中的原生session是无法满足需求的,因为用户的请求有可能随机落入到不同的服务器中,这样的结果将会导致用户的session丢失,传统做法中有解决方 ...
- Oracle学习DayTwo
一.创建表和管理表 1.表名和列名的命名规则 必须以字母开头必须在 1–30 个字符之间必须只能包含 A–Z, a–z, 0–9, _, $, 和 #必须不能和用户定义的其他对象重名必须不能是Orac ...
- Object.assign的用法
工作中使用的Object.assign 类的赋值 var initData = { a:'', b:'' } var oldData = { a:'ww', b:'ee' } var newData ...
- 关于position的一些问题
position属性: static:静止 relative:相对的 fixed:固定的 absolu:绝对的 position的一些实例子如下: HTML: <!DOCTYPE htm ...
- vue中使用ajax
var vue = new Vue({ el:"#vueid", data:{ selectById : "", }, methods:{ yourMethod ...
- 解决Caused by: java.lang.IllegalArgumentException: Result Maps collection does not contain value for com.geek.dao.ContentDao.Integer
mybatis报错:Caused by: java.lang.IllegalArgumentException: Result Maps collection does not contain val ...