这里有一个更详细的版本;通读它会让你对这门语言有更好的感觉。在from collections import namedtuple
import sys
# Python 2/3 compatibility shim
if sys.hexversion < 0x3000000:
inp, rng = raw_input, xrange # Python 2.x
else:
inp, rng = input, range # Python 3.x
def type_getter(type):
"""
Build a function to prompt for input of required type
"""
def fn(prompt):
while True:
try:
return type(inp(prompt))
except ValueError:
pass # couldn't parse as the desired type - try again
fn.__doc__ = "\n Prompt for input and return as {}.\n".format(type.__name__)
return fn
get_int = type_getter(int)
get_float = type_getter(float)
# Student record datatype
Student = namedtuple('Student', ['name', 'mark'])
de

该博客演示了一段Python代码,用于从用户输入中获取学生姓名和成绩,创建Student记录,计算平均分,并找出最高分的学生。代码兼容Python 2和3,使用namedtuple和lambda表达式处理数据。
最低0.47元/天 解锁文章

2689

被折叠的 条评论
为什么被折叠?



