我需要创建一个代码,用户可以输入一定数量的类(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/