Python【每日一问】35
问:
基础题:
从键盘输入4个数字,各数字采用空格分隔,对应为变量x0,y0,x1,y1。计算(x0,y0)和(x1,y1)两点之间的距离,输出结果保留1位小数。
比如,键盘输入:0 1 3 5,屏幕输出:5.0
提高题:
键盘输入小明学习的课程以及考试分数信息,信息之间采用空格分隔,每个课程一行,空格回车结束录入,示例格式如下:
数学 90
语文 95
英语 86
物理 84
生物 87
输出得分最高和最低的课程名称、考试分数,以及所有课程的平均分(保留2位小数)
格式如下:
最高分课程是语文 95,最低分课程是物理 84,平均分是88.4
答:
基础题:
从键盘输入4个数字,各数字采用空格分隔,对应为变量x0,y0,x1,y1。计算(x0,y0)和(x1,y1)两点之间的距离,输出结果保留1位小数。
比如,键盘输入:0 1 3 5,屏幕输出:5.0
方法1:
i = input('输入坐标').split()
x1, x2, x3, x4 = eval(i[0]), eval(i[1]), eval(i[2]), eval(i[3])
dis = pow((pow(x3 - x1, 2) + pow(x4 - x2, 2)), 0.5)
print(dis)
方法2:
variable = list(input("输入四个数字:(空格分隔)").split(' '))
x0,y0,x1,y1 = variable[0],variable[1],variable[2],variable[3]
distance = ((eval(x0)-eval(x1))**2 + (eval(y0)-eval(y1))**2)**0.5
print("{:.1f}".format(distance))
方法3:
from math import sqrt, pow def cal_distance(x1, y1, x2, y2):
return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)) if __name__ == '__main__':
x1, y1, x2, y2 = map(float, input("请输入坐标数字:").split())
print(cal_distance(x1, y1, x2, y2))
方法4:
a = input('input your number:').split()
x0, y0, x1, y1 = int(a[0]), int(a[1]), int(a[2]), int(a[3])
target = float(((y0-y1)**2+(x0-x1)**2)**0.5)
print(target)
方法5:
import math
s1 = input("请输入2个点坐标,用逗号分隔")
lst = s1.split(',')
x0 = int(lst[0])
y0 = int(lst[1])
x1 = int(lst[2])
y1 = int(lst[3])
a1 = int(pow((x0 - x1), 2))
a2 = int(pow((y0 - y1), 2))
leng = math.sqrt(a1 + a2)
print("({0},{1}),({2},{3})之间的距离为{4}" .format(x0, y0, x1, y1, leng))
提高题:
键盘输入小明学习的课程以及考试分数信息,信息之间采用空格分隔,每个课程一行,空格回车结束录入,示例格式如下:
数学 90
语文 95
英语 86
物理 84
生物 87
输出得分最高和最低的课程名称、考试分数,以及所有课程的平均分(保留2位小数)
格式如下:
最高分课程是语文 95,最低分课程是物理 84,平均分是88.4
方法1:
j = input('请输入课程和成绩').split()
k = {}
sum = 0
for i in range(0, len(j), 2):
k[j[i]] = eval(j[i+1])
sum += eval(j[i+1])
k1 = sorted(k.items(),key=lambda k : k[1])
print('最高课和成绩:', k1[-1])
print('最低课和成绩:', k1[0])
print('均值:{:.2f}'.format(sum/(len(j)/2)))
方法2:
info_list = []
scores, subjects = 0, 0
while True:
info = input("请输入小明成绩:(以空格分隔;回车结束录入)")
if info == '':
break
else:
info_list.append(info.split(' '))
scores += eval(info.split(' ')[1])
subjects += 1
info_list.sort(key=lambda x: x[1], reverse=True)
print("最高分课程是{}:{},最低分课程是{}:{},平均分是{:.1f}".format(info_list[0][0], info_list[0][1], info_list[-1][0], info_list[-1][1], scores / subjects))
方法3:
class Student:
name = '姓名'
course = 'none'
course_score = -1 def theHighestScore(self, course_score_list):
return max(course_score_list) def theLowestScore(self, course_score_list):
return min(course_score_list) def theAverageScore(self, course_score_list):
sum = 0
for score in course_score_list:
sum += score average_score = sum / len(course_score_list)
return average_score if __name__ == '__main__':
student = Student()
student.name = '小明' course_score_dict = {} student.course = list(map(str, input("请先输入课程名:").strip().split()))
student.course_score = list(map(float, input("然后请输入课程对应考试分数:").strip().split())) course_score_dict = dict(zip(student.course, student.course_score)) print(course_score_dict)
theHighestScore = student.theHighestScore(student.course_score)
theLowestScore = student.theLowestScore(student.course_score) print('最高分课程是%s %d' % (max(course_score_dict, key=course_score_dict.get), theHighestScore))
print('最低分课程是%s %d' % (min(course_score_dict, key=course_score_dict.get), theLowestScore))
print('平均分是', student.theAverageScore(student.course_score))
方法4:
c_s_list = {} # class & score
sum = 0 # 均值
while True:
a = input('input your class && score:')
if a == 'esc':
for key, value in c_s_list.items():
print(key,value)
sum += int(value) # 均值
max_min = sorted(c_s_list.items(), key=lambda s: s[1])
print('\n得分最高的课程名称:{}考试分数:{}'.format(max_min[-1][0], max_min[-1][1]))
print('得分最低的课程名称:{}考试分数:{}'.format(max_min[0][0], max_min[0][1]))
print('均值:%.1f' % (sum/len(c_s_list)))
break
else:
b = a.split()
c_s_list[b[0]] = b[1]
Python【每日一问】35的更多相关文章
- [python每日一练]--0012:敏感词过滤 type2
题目链接:https://github.com/Show-Me-the-Code/show-me-the-code代码github链接:https://github.com/wjsaya/python ...
- Python每日一练(1):计算文件夹内各个文章中出现次数最多的单词
#coding:utf-8 import os,re path = 'test' files = os.listdir(path) def count_word(words): dic = {} ma ...
- python每日一函数 - divmod数字处理函数
python每日一函数 - divmod数字处理函数 divmod(a,b)函数 中文说明: divmod(a,b)方法返回的是a//b(除法取整)以及a对b的余数 返回结果类型为tuple 参数: ...
- 每日一问:Android 消息机制,我有必要再讲一次!
坚持原创日更,短平快的 Android 进阶系列,敬请直接在微信公众号搜索:nanchen,直接关注并设为星标,精彩不容错过. 我 17 年的 面试系列,曾写过一篇名为:Android 面试(五):探 ...
- 每日一问:谈谈 volatile 关键字
这是 wanAndroid 每日一问中的一道题,下面我们来尝试解答一下. 讲讲并发专题 volatile,synchronize,CAS,happens before, lost wake up 为了 ...
- 每日一问:讲讲 Java 虚拟机的垃圾回收
昨天我们用比较精简的文字讲了 Java 虚拟机结构,没看过的可以直接从这里查看: 每日一问:你了解 Java 虚拟机结构么? 今天我们必须来看看 Java 虚拟机的垃圾回收算法是怎样的.不过在开始之前 ...
- 每日一问:你了解 Java 虚拟机结构么?
对于从事 C/C++ 程序员开发的小伙伴来说,在内存管理领域非常头疼,因为他们总是需要对每一个 new 操作去写配对的 delete/free 代码.而对于我们 Android 乃至 Java 程序员 ...
- 每日一问:LayoutParams 你知道多少?
前面的文章中着重讲解了 View 的测量流程.其中我提到了一句非常重要的话:View 的测量匡高是由父控件的 MeasureSpec 和 View 自身的 `LayoutParams 共同决定的.我们 ...
- 每日一问:简述 View 的绘制流程
Android 开发中经常需要用一些自定义 View 去满足产品和设计的脑洞,所以 View 的绘制流程至关重要.网上目前有非常多这方面的资料,但最好的方式还是直接跟着源码进行解读,每日一问系列一直追 ...
- python每日一练:0007题
第 0007 题: 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码.包括空行和注释,但是要分别列出来. # -*- coding:utf-8 -*- import os def count ...
随机推荐
- Solr插件的弊端
在前文<Solr Update插件自定义条件索引>中,我介绍了如何通过插件的模式,自定义Solr的Update过程.但是在大半年的使用过程中,发现这种方式存在如下弊端. 1.环境难以维护. ...
- 树剖LCA讲解
LCA的类型多种多样,只说我知道的,就有倍增求LCA,tarjin求LCA和树链剖分求LCA,当然,也还有很多其他的方法. 其中最常用,速度最快的莫过于树链剖分的LCA了. 树链剖分,首先字面理解一下 ...
- Spring MVC-视图解析器(View Resolverr)-XML视图解析器(Xml View Resolver)示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_xmlviewresolver.htm 说明:示例基于Spring MVC 4.1 ...
- Ubuntu下调整时区时间
Ubuntu下调整时区时间 学习了:http://blog.csdn.net/jintiaozhuang/article/details/38583031 进行了tzselect错误的修复 学习了:h ...
- iOS 代码方式设置按钮标题、图片的偏移
button.imageEdgeInsets = UIEdgeInsetsMake(0,1 , 2, 3); button.titleEdgeInsets = UIEdgeInsetsMake(0,1 ...
- oc42--引用计数器
/* main.m 堆里面的内存释放是根据引用计数器,所以就是操作引用计数器. 创建一个对象,对象里面就有一个引用计数器,有多少指针指向它. 引用计数器为0就释放.任何一个对象初始化时就是1,所以 { ...
- rk3288对于parameter参数文件的解析处理【转】
本文转载自:http://blog.csdn.net/groundhappy/article/details/56479694 rk3288有一个parameter文件. 类似于 FIRMWARE_V ...
- 关于FrameBuffer【转】
本文转载自:http://blog.csdn.net/ganxingming/article/details/764482 一.FrameBuffer的原理 FrameBuffer 是出现在 2.2. ...
- WinForm c# 备份 还原 数据库(Yc那些事儿 转)
Yc那些事儿 我愿意 为了我的幸福 奋斗终生 2008-11-17 18:04 WinForm c# 备份 还原 数据库 其实是个非常简单的问题,一个Form,一个Button,一个OpenF ...
- IJ:ALI OSS 配置
ylbtech-IJ:ALI OSS 配置 1. src/resources/返回顶部 1.src/resources/ 1.1.aliyunoss.properties # oss\u7684\u5 ...