为了账号安全,请及时绑定邮箱和手机立即绑定

哪里出错了?

d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }

def generate_tr(name, score):

    return '<tr><td>%s</td><td>%s</td></tr>' % (name, score)


tds = [generate_tr(name, score) for name, score in d.iteritems()]

print '<table border="1">'

if score>=60

    print '<tr><th>Name</th><th>Score</th><tr>'

else

    print '<tr><th>Name</th><td style="color:red"><th>Score</th><tr>'

print '\n'.join(tds)

print '</table>'

执行完说正确 但是右边并没有编译出表格啊

这是为什么

学得不好  还请大神指导下

正在回答

4 回答

题主让我有点不知道是不头晕感觉,if判断句应该在循环体内,险答案外,以下代码可能符合题主想法

d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }
tds=[]
for name,score in d.iteritems():
	if score>=60:
		tds.append('<tr><td>%s</td><td>%s</td></tr>' % (name,score))
	else:
		tds.append('<tr><td>%s</td><td style="color:red">%s</td></tr>' % (name,score))
print '<table border="1">'
print '<tr><th>Name</th><th>Score</th></tr>'
print '\n'.join(tds)
print '</table>'


0 回复 有任何疑惑可以回复我~

http://img1.sycdn.imooc.com//56f93f240001f80606620256.jpg

你的代码中,

代码块1实现了把生成了一个列表对象tds, 具体内容如下:

['<tr><td>%Adam</td><td>%95</td></tr>' , '<tr><td>%Lisa</td><td>%85</td></tr>' , '<tr><td>%Bart</td><td>%59</td></tr>']

······················································································································································································

代码块2,我实在不知道有什么用,if语句中判断的score有具体指向吗?

score首先不是一个对象,不能直接对其判断,其次,score在这个题目中是一个用在for循环中的临时抽象的名称,不能不直接判断吧。

···················································································································································································

代码块3  join()函数的功能,是把一个list对象拼接成一个字符串对象

·····················································································································································································

正确的代码的逻辑应该是这样的: 在通过for循环生成dts列表对象的时候,使对应分数是59的那个列表元素就变成这样一个字符串 '<tr><td>%Bart</td><td style="color:red">%59</td></tr>'  

0 回复 有任何疑惑可以回复我~

generate_tr   在这个函数里面去实现  效果 如 不及格 数字显示红色

0 回复 有任何疑惑可以回复我~

d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }

def generate_tr(name, score):

    return '<tr><td>%s</td><td>%s</td></tr>' % (name, score)


tds = [generate_tr(name, score) for name, score in d.iteritems()]

print '<table border="1">'

print '\n'.join(tds)

print '</table>'


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
初识Python
  • 参与学习       758623    人
  • 解答问题       8667    个

学python入门视频教程,让你快速入门并能编写简单的Python程序

进入课程

哪里出错了?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信