Python 有人能解释下面的代码是如何工作的吗?
我是新来的,目前正在学习python。下面的代码是一个平均高度计算器。如果有人能告诉我怎么做,我将不胜感激Python 有人能解释下面的代码是如何工作的吗?,python,python-3.x,Python,Python 3.x,我是新来的,目前正在学习python。下面的代码是一个平均高度计算器。如果有人能告诉我怎么做,我将不胜感激 for n in range(0, len(student_heights)): student_heights[n] = int(student_heights[n]) 工作,以及变量总高度和学生人数如何像函数sum和len一样工作?谢谢大家的回答,这对我的学习有很大帮助 ### I don't understand the code below ### student_heigh
for n in range(0, len(student_heights)):
student_heights[n] = int(student_heights[n])
工作,以及变量总高度和学生人数如何像函数sum和len一样工作?谢谢大家的回答,这对我的学习有很大帮助
### I don't understand the code below ###
student_heights = input("Input a list of student heights ").split()
for n in range(0, len(student_heights)):
student_heights[n] = int(student_heights[n])
total_height = 0
for height in student_heights:
total_height += height
print(f"total height = {total_height}")
number_of_students = 0
for student in student_heights:
number_of_students += 1
print(f"number of students = {number_of_students}")
### I don't understand the code above ###
average_height = round(total_height / number_of_students)
print(average_height)
student_heights=input(“输入学生身高列表”).split()
对于范围(0,len(学生身高))内的n:
学生身高[n]=int(学生身高[n])
从用户获取一个由空格分隔的数字字符串,在空格处拆分该字符串,并将每个结果字符串转换为一个数字
总高度=0
对于学生身高:
总高度+=高度
打印(f“总高度={总高度}”)
计算所有学生的身高并打印总身高
学生人数=0
对于学生身高的学生:
学生人数+=1
打印(f“学生人数={学生人数}”)
计算学生人数,并打印数字
第2部分可以替换为
总高度=总和(学生高度)
打印(f“总高度={总高度}”)
第3部分可以替换为
学生人数=len(学生身高)
打印(f“学生人数={学生人数}”)
但实际上,这种方式不需要中间变量,只需直接打印值即可。第2+3部分:
print(f“总高度={sum(学生身高)}”)
打印(f“学生人数={len(学生身高)}”)
这不是使用堆栈溢出的方式,因此您可能会收到一些反对票。然而,我非常喜欢阅读代码并帮助他人,所以我们开始吧
student_heights = input("Input a list of student heights ").split()
for n in range(0, len(student_heights)):
student_heights[n] = int(student_heights[n])
这段代码根据输入的内容创建一个高度列表。由于输入是一个字符串,因此我们将每个高度转换为一个整数,并将其存储回列表中。(如果您输入的内容不是整数,则会中断并显示错误)。.split()
以任意空格分隔输入,因此,例如,输入“50 60 42”将被拆分为“50”、“60”和“42”的列表。然后循环将其转换为50、60和42的列表-注意缺少引号,因为它们现在是整数
total_height = 0
for height in student_heights:
total_height += height
print(f"total height = {total_height}")
这个位循环通过我们刚刚转换为整数的每个高度,并将它们相加,因此total_height等于列表中每个值的总和。+=
表示“此值+”-因此总高度+=高度
与总高度=总高度+高度
相同。如果我们有一个50、60和42的列表,那么我们可以按如下步骤遍历代码:
First iteration: total_height = 0, height = 50
total_height = total_height + height
total_height = 0 + 50
total_height = 50
Second iteration: total_height = 50, height = 60
total_height = total_height + height
total_height = 50 + 60
total_height = 110
Third iteration: total_height = 110, height = 42
total_height = total_height + height
total_height = 110 + 42
total_height = 152
如果你没有得到这一点,请随时发表评论
number_of_students = 0
for student in student_heights:
number_of_students += 1
print(f"number of students = {number_of_students}")
该位再次循环遍历列表中的每个高度,但这一次计算有多少高度-其结果与执行len(学生高度)
相同。使用与前面相同的示例(50、60、42):
再说一次,如果这里有什么不合理的地方,请告诉我
average_height = round(total_height / number_of_students)
print(average_height)
现在我们已经把所有的高度加在一起,计算了我们有多少学生,我们可以把这些数字除以,然后四舍五入,得到一个更好的数字。使用之前的示例(对于60,50,42的列表):
在这种情况下,程序输出51。您不明白其中的哪一部分?@Barmar他清楚地指出了这一点!听起来您需要阅读Python
for
循环的教程。它循环student\u heights
中的每个条目,并使用assplit()
-ing来自input()的值将其转换为整数格式
将返回不能/不应该用于算术运算的字符串类型的变量。似乎使用for
循环来计数学生是一种困难的方法,因为您可以使用number\u of_students=len(student\u heights)
。
average_height = round(total_height / number_of_students)
print(average_height)
average_height = round(total_height / number_of_students)
average_height = round(152 / 3)
average_height = round(50.66666666666666666666666)
average_height = 51