谁能解释一下下面的代码是如何工作的?

时间:2021-02-16 20:18:46

标签: python python-3.x

我是新手,目前正在学习 Python。下面的代码是一个平均身高计算器。如果有人能向我解释如何

,我将不胜感激
for n in range(0, len(student_heights)):
  student_heights[n] = int(student_heights[n])

工作,变量 total_height 和 number_of_students 如何像函数 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)

2 个答案:

答案 0 :(得分:2)

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}")

计算有多少学生,并打印出来。

第 2 部分可以替换为

total_height = sum(student_heights)
print(f"total height = {total_height}")

和第 3 部分可以替换为

number_of_students = len(student_heights)
print(f"number of students = {number_of_students}")

但实际上,这种方式不需要中间变量,直接打印值即可。第 2 + 3 部分:

print(f"total height = {sum(student_heights)}")
print(f"number of students = {len(student_heights)}")

答案 1 :(得分:1)

这不是 Stack Overflow 的使用方式,因此您可能会收到一些反对票。但是,我非常喜欢通读代码并帮助他人,所以我们开始吧!


    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 等于列表中每个值的总和。 += 表示“这个值 +” - 所以 total_height += heighttotal_height = total_height + 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(student_heights) 相同。使用与之前相同的示例 (50, 60, 42):

    First iteration: num_students = 0, student = 50
    num_students = num_students + 1
    num_students = 0 + 1
    num_students = 1

    Second iteraation: num_students = 1, student = 60
    num_students = num_students + 1
    num_students = 1 + 1
    num_students = 2
    
    Third iteration: num_students = 2, student = 42
    num_students = num_students + 1
    num_students = 2 + 1
    num_students = 3

再说一次,如果这里有什么不合理的地方,请告诉我!


    average_height = round(total_height / number_of_students)
    print(average_height)

既然我们已经将所有的高度加在一起,并计算了我们有多少学生,我们可以将这些数字相除,然后将它们四舍五入得到一个更好的数字。使用我们之前的示例(对于 60,50,42 的列表):


    average_height = round(total_height / number_of_students)
    average_height = round(152 / 3)
    average_height = round(50.66666666666666666666666)
    average_height = 51

在这种情况下,程序输出 51。

相关问题