Python [Q] 35 a day

ask:

Basic questions:

从键盘输入4个数字,各数字采用空格分隔,对应为变量x0,y0,x1,y1。计算(x0,y0)和(x1,y1)两点之间的距离,输出结果保留1位小数。
比如,键盘输入:0 1 3 5,屏幕输出:5.0

Raise the question:

键盘输入小明学习的课程以及考试分数信息,信息之间采用空格分隔,每个课程一行,空格回车结束录入,示例格式如下:
数学 90
语文 95
英语 86
物理 84
生物 87
输出得分最高和最低的课程名称、考试分数,以及所有课程的平均分(保留2位小数)
格式如下:
最高分课程是语文 95,最低分课程是物理 84,平均分是88.4

答:

Basic questions:

从键盘输入4个数字,各数字采用空格分隔,对应为变量x0,y0,x1,y1。计算(x0,y0)和(x1,y1)两点之间的距离,输出结果保留1位小数。
比如,键盘输入:0 1 3 5,屏幕输出:5.0

 

method 1:

 

INPUT = I ( ' input coordinate ' ) .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)

 

Method 2:

 

List = variable (INPUT ( " Enter :( four numbers separated by spaces) " ) .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))

 

Method 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 (a float, INPUT ( " Please enter the number coordinate: " ) .split ())
     Print (cal_distance (X1, Y1, X2, Y2))

 

Method 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)

 

Method 5:

 

import math

S1 = INPUT ( " Please enter the coordinates of two points, separated by commas " )
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} between the distance). 4} to { " .format (X0, yO, X1, Y1, Leng))

Raise the question:

键盘输入小明学习的课程以及考试分数信息,信息之间采用空格分隔,每个课程一行,空格回车结束录入,示例格式如下:
数学 90
语文 95
英语 86
物理 84
生物 87
输出得分最高和最低的课程名称、考试分数,以及所有课程的平均分(保留2位小数)
格式如下:
最高分课程是语文 95,最低分课程是物理 84,平均分是88.4


方法1:

the INPUT = J ( ' Please enter the course and results ' ) .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 = the sorted (k.items (), Key = the lambda K: K [. 1 ])
 Print ( ' highest class and results: ' , K1 [-1 ])
 Print ( ' lowest class and results: ' , K1 [0] )
 Print ( ' mean: {}. 2F :. ' .format (SUM / (len (J) / 2)))

方法2:

info_list = []
scores, subjects = 0, 0
while True:
    info = INPUT ( " Please enter Xiaoming results :( separated by spaces; carriage return entry) " )
     IF info == '' :
         BREAK 
    the else :
        info_list.append(info.split(' '))
        scores += eval(info.split(' ')[1])
        Subjects + =. 1 
info_list.sort (Key = the lambda X: X [. 1], Reverse = True)
 Print ( " highest score course is {}: {}, {} is the lowest score course: {}, {Average rating is: } .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 = ' Hsiao Ming '

    course_score_dict = {}

    student.course = List (the Map (str, the INPUT ( " Please enter the program name: " ) .strip () Split ()).)
    student.course_score = List (the Map (float, the INPUT ( " and then enter the corresponding course test scores: " ) .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 ( ' highest score course is S% D% ' % (max (course_score_dict, Key = course_score_dict.get), theHighestScore))
     Print ( ' lowest points program is S% D% ' % (min (course_score_dict, Key = course_score_dict. GET), theLowestScore))
     Print ( ' average was ' , student.theAverageScore (student.course_score))

 

Method 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-highest score Course: test scores {}: {} ' .format (MAX_MIN [-1] [0], MAX_MIN [-1] [. 1 ]))
         Print ( ' lowest score Course: test scores {}: {} ' .format (MAX_MIN [0] [0], MAX_MIN [0] [. 1 ]))
         Print ( ' mean:. 1F% ' % (SUM / len (c_s_list)))
         BREAK 
    the else :
        b = a.split()
        c_s_list[b[0]] = b[1]

 

 

 

 

 



Guess you like

Origin www.cnblogs.com/ElegantSmile/p/10989017.html