python - 如何在python循环中不断增加变量数量

标签 python variables loops python-3.x

我需要创建一个代码,用户可以输入一定数量的类(class),然后它将获取它们的gpa,但是如何更改循环中的变量名称? 到目前为止我已经有了

number_courses= float(input ("Insert the number of courses here:"))
while number_courses>0:
    mark_1= input("Insert the letter grade for the first course here: ")
    if mark_1=="A+" :
        mark_1=4.0
    number_courses= number_courses-1

如果我想在每次循环时将 mark_one 的变量名更改为不同的名称,最简单的方法是什么?并且是否可以在我的输入语句中更改它以在循环时询问第一,第二,第三......?我尝试在谷歌上搜索,但没有一个我能理解的答案,因为他们的代码远远落后于我的水平,或者他们似乎也没有回答我需要的东西。谢谢。

最佳答案

您想使用list或类似收集输入值的东西:

number_courses=input("Insert the number of courses here: ")
marks = []
while number_courses>0:
    mark = input("Insert the letter grade for the first course here: ")
    if mark == "A+":
        mark = 4.0
    marks.append(mark)
    number_courses -= 1

print marks

关于python - 如何在python循环中不断增加变量数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19080685/

相关文章:

python - 使用 matplotlib python 绘制决策树分类器的 2 个以上特征

variables - Fortran 中的变量类型

php - 正则表达式 $1、$2 等

php - 使用范围解析运算符(::) 分配变量与 $this->variable 相同吗?

c++ - 在 C++ 中循环遍历字符串

python - Alembic,如何更改修订数据库的名称?

python - 为什么 +(加号)可以在 Python 中连接两个字符串?

python - 为什么我的 python 多进程代码运行速度很慢?

Jquery 循环选择器和单击事件处理程序

php - 如何使用循环插入多个值?