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 ...
随机推荐
- php ip伪装访问
打算做个采集,无记录下来备用 php的curl搞定ip伪装来采集内容.以前写过一段代码采集一个数据来处理.由于数据量过大,同一ip采集.经常被限制,或者列为黑名单. 写了段代码伪装ip,原理是,客 ...
- [bzoj4066/2683]简单题_KD-Tree
简单题 bzoj-4066 题目大意:n*n的棋盘,开始为均为0,支持:单点加权值,查询矩阵权值和,强制在线. 注释:$1\le n\le 5\cdot 10^5$,$1\le m \le 2\cdo ...
- P1464 Function 洛谷
https://www.luogu.org/problem/show?pid=1464 题目描述 对于一个递归函数w(a,b,c) 如果a<=0 or b<=0 or c<=0就返回 ...
- BlockQueue中ArrayBlockingQueue和LinkedBlockingQueue比较
LinkedBlockingQueue是BlockingQueue的一种使用Link List的实现,它对头和尾(取和添加操作)采用两把不同的锁,相对于ArrayBlockingQueue提高了吞吐量 ...
- [转]数据库查询 sysobjects
sysobjects sysobjects是系统自建的表,里面存储了在数据库内创建的每个对象(约束.默认值.日志.规则.存储过程等),各在表中占一行.只有在 tempdb 内,每个临时对象才在该表中占 ...
- HDU 4522
DIJK,最短路,建两个图就好了. #include <cstdlib> #include <cstdio> #include <cstring> #include ...
- [iOS]怎样在iOS开发中切换显示语言实现国际化
1.在Project设置,加入中英两种语言: 2.新建Localizable.strings文件,作为多语言相应的词典,存储多种语言,点击右側Localization,勾选中英: watermark/ ...
- Python常用模块【sys】
sys.argv 参数 「argv」是「argument variable」参数变量的简写形式.一般在命令行调用的时候由系统传递给程序.这个变量其实是一个List列表,argv[0] 一般是“被 ...
- ASP.NET MVC 认证模块报错:“System.Configuration.Provider.ProviderException: 未启用角色管理器功能“
新建MVC4项目的时候 选 Internet 应用程序的话,出来的示例项目就自带了默认的登录认证等功能.如果选空或者基本,就没有. 如果没有,现在又想加进去,怎么办呢? 抄啊.将示例项目的代码原原本本 ...
- android自定义dialog中点击listview的item事件关闭dialog
import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; ...