我试图让用户输入他们有多少个课程(x),问“您在这些课程中的成绩是多少?” x次,并记录所有输入的成绩供以后使用。
我试图将问题分配给变量,并要求打印该变量,但是我只得到最后输入的数字。我不想打印数字,我想将它们存储起来以便以后将它们加在一起。我只是在使用print函数来查看如果分配变量实际有效,我的数字将如何存储。我将如何记录所有输入的数字以供以后添加和计算GPA?
numofclasses = int(input("How many honors classes do you have?: "))
for i in range(numofclasses):
grades = str(input("Enter the unweighted grade from one class "))
print(grades)
我想记录所有输入的数字,但是使用print
选项,我只会记录最后输入的数字。
答案 0 :(得分:2)
您要使用的是list,它用于容纳一系列数据类型(例如整数,字符等)的容器
这样想,如果您想在python中使用3个变量,通常会做什么
a = 1
b = 2
c = 3
这很好用,但是如果变量数是50或100,那么您将继续定义多少个变量,因此您将需要一个容器来存储这些变量,这就是列表的所在。做
li = [1,2,3]
并通过从0开始的索引访问这些变量
a[0] #1
a[1] #2
a[2] #3
记住这一点,我们会做的!
numofclasses = int(input("How many honors classes do you have?: "))
#List to save all grades, defined by assigning variable to []
all_grades = []
for i in range(numofclasses):
#Take grades from the user
grades = input("Enter the unweighted grade from one class ")
#Append the grades to the list, using list.append function
all_grades.append(grades)
#Loop through the list to print it
for item in all_grades:
print(item)
#Print all grades in a single line by joining all items of list in a string
s = " ".join(all_grades)
print(s)
输出看起来像
How many honors classes do you have?: 3
Enter the unweighted grade from one class A
Enter the unweighted grade from one class B
Enter the unweighted grade from one class C
#All grades in different lines
A
B
C
#All grades in single line
A B C
答案 1 :(得分:0)
您可以使用list
来存储成绩。在循环的每次迭代中,您都可以使用内置的append
函数将新成绩添加到此列表中。
grades = []
numofclasses = int(input("How many honors classes do you have?: "))
for i in range(numofclasses):
grade = str(input("Enter the unweighted grade from one class "))
grades.append(grade)
print(grades)
输出
How many honors classes do you have?: 5
Enter the unweighted grade from one class 75
Enter the unweighted grade from one class 80
Enter the unweighted grade from one class 90
Enter the unweighted grade from one class 55
Enter the unweighted grade from one class 79
['75','80','90','55','79']
答案 2 :(得分:0)
在我看来,有两种选择可能是合适的。
在每次迭代中打印输入:
numofclasses = int(input("How many honors classes do you have?: "))
for i in range(numofclasses):
grades = str(input("Enter the unweighted grade from one class "))
print(grades) # move print to inside of loop
将值存储在列表中以供以后打印:
numofclasses = int(input("How many honors classes do you have?: "))
grades = []
for i in range(numofclasses):
grades.append(str(input("Enter the unweighted grade from one class ")))
print(grades) # will look like ["A", "B", "C", "B"]
答案 3 :(得分:0)
这是您的操作方式:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMySql("Server=localhost;Database=myDbUid=myUserId;");
}
以上应该做到。