Python 如何获取整数列表,获取函数字母“u grade”并打印';成绩为A';例如
这是我的代码,我想知道如何获取整数(分数)列表,调用函数并打印一些像“分数是a”之类的内容Python 如何获取整数列表,获取函数字母“u grade”并打印';成绩为A';例如,python,Python,这是我的代码,我想知道如何获取整数(分数)列表,调用函数并打印一些像“分数是a”之类的内容 def letter_grade(): score = input('Enter your test score: ') if score < 60: print 'The grade is E' elif score < 70: print 'The grade is D' elif score < 80: print 'The grade is C' elif
def letter_grade():
score = input('Enter your test score: ')
if score < 60:
print 'The grade is E'
elif score < 70:
print 'The grade is D'
elif score < 80:
print 'The grade is C'
elif score < 90:
print 'The grade is B'
else:
print 'The grade is A'
return score
letter_grade()
def letter_grade():
分数=输入('输入您的考试分数:')
如果得分<60:
打印“成绩为E”
elif评分<70分:
打印“成绩为D”
elif评分<80分:
打印“成绩为C”
elif评分<90分:
打印“成绩为B”
其他:
打印“成绩为A”
回击得分
字母_等级()
首先让函数接受一个参数
def letter_grade(score):
if score < 60:
print 'The grade is E'
elif score < 70:
print 'The grade is D'
elif score < 80:
print 'The grade is C'
elif score < 90:
print 'The grade is B'
else:
print 'The grade is A'
return score
score = int(raw_input('Enter your test score: '))
letter_grade(score)
请注意,我们现在使用格式
将分数插入字符串。现在查看分数的列表
list_of_scores = range(50, 100, 5) # a list of scores [50, 55, 60, 65, 70, 75, 80, 85, 90, 95]
for score in list_of_scores:
print "The grade is {}".format(letter_grade(score))
首先,让函数接受一个参数
def letter_grade(score):
if score < 60:
print 'The grade is E'
elif score < 70:
print 'The grade is D'
elif score < 80:
print 'The grade is C'
elif score < 90:
print 'The grade is B'
else:
print 'The grade is A'
return score
score = int(raw_input('Enter your test score: '))
letter_grade(score)
请注意,我们现在使用格式
将分数插入字符串。现在查看分数的列表
list_of_scores = range(50, 100, 5) # a list of scores [50, 55, 60, 65, 70, 75, 80, 85, 90, 95]
for score in list_of_scores:
print "The grade is {}".format(letter_grade(score))
如果代码具有适当的缩进,则代码是正确的。给定一个整数列表,可以使用定义为“ABC”类分数的for循环和字母_grade()。它是正确的吗?我实际上在尝试使用数字分数,比如97、84、32、67等等。如果代码有适当的缩进,那么您的代码是正确的。给定一个整数列表,可以使用定义为“ABC”类分数的for循环和字母_grade()。这是正确的吗?我实际上在尝试使用数字分数,比如97、84、32、67等。请记住,
str.format()
是在Python 2.6中添加的,因此如果您使用的是早期版本(希望不是),您可能会使用类似于print”的东西,分数是%s”%letter\u grade(分数)
这在以后的版本中也会起作用。@MattDMo,是的,但是如果有人学习Python只是为了向后兼容:)我在范围中输入数字时出错()类型错误:范围最多需要3个参数,得到4个参数,而且它只打印范围中的第一个数字()记住str.format()
是在Python2.6中添加的,因此如果您使用的是早期版本(希望不是),您可能会使用类似于print“grade is%s”%letter\u grade(score)
的东西,顺便说一句,它在以后的版本中也会起作用。@MattDMo,对,但是,如果有人学习Python只是为了向后兼容:)我在将数字放入range()时出错。TypeError:range最多需要3个参数,got 4它也只打印range()中的第一个数字