marks = {}
for _ in range(int(input())):
line = input().split()
marks[line[0]] = list(map(float, line[1:]))
print('%.2f' %(sum(marks[input()])/3))
我是python的新手。你能告诉我这段代码的意思吗?
我听不懂。
答案 0 :(得分:4)
此代码的作用:
# initialized a dictionary type names marks
marks = {}
# The input() method will pause and wait for someone to input data in the command line
# The range() method will create an array of int given the a number
# example: range(5) will create [0, 1, 2, 3, 4]
# In this case it will take the string returned from input() convert it to an integer
# and use that as the value.
# The for loop will, run as many times as there are elements "in" the array created
# the _ is just a silly variable name the developer used because
# he is not using the value in the array anywhere.
for _ in range(int(input())):
# Get a new input from the user
# split the string (it uses spaces to cut the string into an array)
# example if you type "one two three" it will create ["one", "two", "three"]
# store the array in the variable line
line = input().split()
# add/replace the element using the first string in the line as key
# line[0] is the first element in the array
# lint[1:] is the array containing all the elements starting at index 1 (the second element)
# map() is a function that will call the function float on each elements of the array given. basically building an array with the values [float(line[1]), float(line[2])…]
# list will convert the array into a list.
marks[line[0]] = list(map(float, line[1:]))
# this last line asks the user for one more value
# gets the list in the marks dictionary using the value inputed by the user
# calculates the sum of all the floats in that list.
# divides it by 3 and prints the results as a floating point number with 2 decimal places.
print('%.2f' %(sum(marks[input()])/3))