在 python 中使用 For 循环使其语法正确
Using For loop in python to make it grammatically correct
一名体操运动员可以从每位裁判那里获得 1 到 10 之间的分数;没有更低,没有更高。所有分数均为整数值;单个法官没有十进制分数。将体操运动员从一位裁判那里获得的可能得分存储在一个元组中。打印出句子:
"The lowest possible score is ____, and the highest possible score is
____."
使用元组中的值。打印出一系列句子,"A judge can give a gymnast _ points."
我的解决方案:
scores = (1,2,3,4,5,6,7,8,9,10)
for num in scores:
print('A judge can give a gymnast %d points.' % (num))
输出:
A judge can give a gymnast 1 points.
A judge can give a gymnast 2 points.
A judge can give a gymnast 3 points.
A judge can give a gymnast 4 points.
A judge can give a gymnast 5 points.
A judge can give a gymnast 6 points.
A judge can give a gymnast 7 points.
A judge can give a gymnast 8 points.
A judge can give a gymnast 9 points.
A judge can give a gymnast 10 points.
如何更改第一行使其语法正确"A judge can give a gymnast 1 point"?
如果数字大于 1
,您可以使用条件表达式仅将 's'
添加到 'point'
。另请注意,使用 range()
比手动输入分数更整洁,.format
比 %
更好(尤其是在使用多种格式时)。
for num in range(1, 11):
print('A judge can give a gymnast {} point{}.'.format(num, 's' if num > 1 else ''))
给出:
A judge can give a gymnast 1 point.
A judge can give a gymnast 2 points.
A judge can give a gymnast 3 points.
A judge can give a gymnast 4 points.
A judge can give a gymnast 5 points.
A judge can give a gymnast 6 points.
A judge can give a gymnast 7 points.
A judge can give a gymnast 8 points.
A judge can give a gymnast 9 points.
A judge can give a gymnast 10 points.
您可以使用 python 3.6:
中的 f-strings
scores = (1,2,3,4,5,6,7,8,9,10)
for num in scores:
print(f'A judge can give a gymnast {num} point{"s" if num > 1 else ""}.')
输出:
A judge can give a gymnast 1 point.
A judge can give a gymnast 2 points.
A judge can give a gymnast 3 points.
A judge can give a gymnast 4 points.
A judge can give a gymnast 5 points.
A judge can give a gymnast 6 points.
A judge can give a gymnast 7 points.
A judge can give a gymnast 8 points.
A judge can give a gymnast 9 points.
A judge can give a gymnast 10 points.
一名体操运动员可以从每位裁判那里获得 1 到 10 之间的分数;没有更低,没有更高。所有分数均为整数值;单个法官没有十进制分数。将体操运动员从一位裁判那里获得的可能得分存储在一个元组中。打印出句子:
"The lowest possible score is ____, and the highest possible score is ____."
使用元组中的值。打印出一系列句子,"A judge can give a gymnast _ points."
我的解决方案:
scores = (1,2,3,4,5,6,7,8,9,10)
for num in scores:
print('A judge can give a gymnast %d points.' % (num))
输出:
A judge can give a gymnast 1 points.
A judge can give a gymnast 2 points.
A judge can give a gymnast 3 points.
A judge can give a gymnast 4 points.
A judge can give a gymnast 5 points.
A judge can give a gymnast 6 points.
A judge can give a gymnast 7 points.
A judge can give a gymnast 8 points.
A judge can give a gymnast 9 points.
A judge can give a gymnast 10 points.
如何更改第一行使其语法正确"A judge can give a gymnast 1 point"?
如果数字大于 1
,您可以使用条件表达式仅将 's'
添加到 'point'
。另请注意,使用 range()
比手动输入分数更整洁,.format
比 %
更好(尤其是在使用多种格式时)。
for num in range(1, 11):
print('A judge can give a gymnast {} point{}.'.format(num, 's' if num > 1 else ''))
给出:
A judge can give a gymnast 1 point.
A judge can give a gymnast 2 points.
A judge can give a gymnast 3 points.
A judge can give a gymnast 4 points.
A judge can give a gymnast 5 points.
A judge can give a gymnast 6 points.
A judge can give a gymnast 7 points.
A judge can give a gymnast 8 points.
A judge can give a gymnast 9 points.
A judge can give a gymnast 10 points.
您可以使用 python 3.6:
中的 f-stringsscores = (1,2,3,4,5,6,7,8,9,10)
for num in scores:
print(f'A judge can give a gymnast {num} point{"s" if num > 1 else ""}.')
输出:
A judge can give a gymnast 1 point.
A judge can give a gymnast 2 points.
A judge can give a gymnast 3 points.
A judge can give a gymnast 4 points.
A judge can give a gymnast 5 points.
A judge can give a gymnast 6 points.
A judge can give a gymnast 7 points.
A judge can give a gymnast 8 points.
A judge can give a gymnast 9 points.
A judge can give a gymnast 10 points.