【发布时间】:2014-11-30 19:39:12
【问题描述】:
我正在根据放入 CSV 文件的这组信息 (http://www.databasebasketball.com/players/playerlist.htm) 编写代码。
我想做一个代码,确定每个玩家的BMI,然后如果他们的BMI超过30,就会认为他们肥胖。
我如何定义一个函数来返回每个玩家的信息,而不仅仅是一个?
import csv
def read_csv(filename):
"""
Reads a Comma Separated Value file,
returns a list of rows; each row is a dictionary of columns.
"""
with open(filename, encoding="utf_8_sig") as file:
reader = csv.DictReader(file)
rows = list(reader)
return rows
# Try out the function
players = read_csv("players.csv")
# Print information on the first player, to demonstrate how
# to get to the data
from pprint import pprint
pprint(players[0])
print(players[0]["lastname"])
print(players[0]["weight"])
total_h_inches = int(players[0]["h_feet"]) * 12 + int(players[0]["h_inches"])
def obesity(bmi):
bmi=(int(players[0]["weight"])/(total_h_inches**2))* 703
if bmi >= 30:
print ('player', players[0]["lastname"], 'is obese')
else:
print (('player', players[0]["lastname"], 'is not obese'))
print(obesity([0]))
它返回第一个玩家的信息,但我不确定如何编辑代码以便它适用于任何玩家
【问题讨论】: