【问题标题】:python beginner : Dynamically modifying a python list with indexing/slicing to preform arithmetic operatorspython 初学者:使用索引/切片动态修改 python 列表以执行算术运算符
【发布时间】:2023-01-13 00:53:30
【问题描述】:

我一直在尝试编写代码,以动态地从列表中获取用户输入并执行通用算术运算符。为了解决这个问题,我使用了索引和切片,这确实暂时解决了我的问题,但是这样做又产生了一个新问题。

listgrades= []

num_students = int(input("How many students are you evaluating?"))

def student_info():
    for i in range(0, num_students):
        student_name=input("Enter your name here: ")
        studnet_age=input("Enter your age here: ")
        student_total_grade=int(float(input("What is your total grade")))
        listgrades.append(student_total_grade)

student_info()
grades_sum= (listgrades[0] + listgrades[1] + listgrades[2]) / num_students
print(f"The average of all the student grades is {grades_sum}")

`

我正在尝试改变(列表等级[0] + 列表等级[1] + 列表等级[2])更易变、更可行和可扩展的东西

我试图寻找解决方案或方法来解决这个问题,但我走到了死胡同,此时我已经没有想法了。

我认为某种循环可能适用于此,但我不确定。

旁注:我有点研究了 numpy 但我不能使用它,因为我的学校实验室计算机不允许默认 python 模块库中的任何内容。

【问题讨论】:

标签: python-3.x list loops dynamic slice


【解决方案1】:

只需使用sum函数来总结学生成绩:

grades_avg = sum(listgrades) / num_students

【讨论】:

  • 问题“我正在尝试将 (listgrades[0] + listgrades[1] + listgrades[2]) 更改为更具可变性、可行性和可扩展性的东西”这里肯定有很多重复项......拥有与您一样多的代表(和金蟒蛇徽章)的用户应该知道在回答之前寻找重复项
猜你喜欢
  • 1970-01-01
  • 2011-07-17
  • 2018-01-09
  • 2017-08-07
  • 2016-06-13
  • 1970-01-01
  • 2013-09-06
  • 1970-01-01
  • 2023-03-05
相关资源
最近更新 更多