Python 使用“时如何记录输入”;For Loop";
我试图让用户输入他们有多少个班级(x),询问“你在这些班级中的分数是多少?”x次,然后记录所有输入的分数以供以后使用 我试图将问题分配给一个变量,并要求打印变量,但我只得到最后输入的数字。我不想打印这些数字,我想把它们存储起来,以便以后可以把它们加在一起。我只是使用print函数来查看如果赋值变量实际起作用,我的数字将如何存储。我如何记录所有输入的数字,以便以后添加和计算GPAPython 使用“时如何记录输入”;For Loop";,python,for-loop,Python,For Loop,我试图让用户输入他们有多少个班级(x),询问“你在这些班级中的分数是多少?”x次,然后记录所有输入的分数以供以后使用 我试图将问题分配给一个变量,并要求打印变量,但我只得到最后输入的数字。我不想打印这些数字,我想把它们存储起来,以便以后可以把它们加在一起。我只是使用print函数来查看如果赋值变量实际起作用,我的数字将如何存储。我如何记录所有输入的数字,以便以后添加和计算GPA numofclasses = int(input("How many honors classes do you ha
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)
我想记录所有输入的数字,但通过使用
打印
选项,我只记录最后一个输入的数字。您要使用的是一个,用于保存一系列数据类型(如整数、字符等)的容器
这样想,如果你想在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
您要使用的是一个容器,用于保存一系列数据类型,如整数、字符等 这样想,如果你想在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
在我看来,有几个选择可能是合适的 每次迭代打印输入:
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"]
在我看来,有几个选择可能是合适的 每次迭代打印输入:
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"]
以下是你如何做到这一点:
class_dict = {}
numOfClasses = input("How many classes do you take? Enter here : ")
for i in range(int(numOfClasses)):
class_dict["class" + str(i +1)] = input("Enter your grade for class " + str(i +1) + ": ")
print(class_dict)
以上内容应该可以做到这一点。以下是您如何做到这一点:
class_dict = {}
numOfClasses = input("How many classes do you take? Enter here : ")
for i in range(int(numOfClasses)):
class_dict["class" + str(i +1)] = input("Enter your grade for class " + str(i +1) + ": ")
print(class_dict)
以上内容就可以了。非常感谢您的帮助!如果我使用第二个选项,是否有一种方法可以将所有输入的分数(a、B、C、D)相加?是的,类似于打印(“,”。连接(分数))(未经测试,因为我在手机上,但应该没有打字错误),“,”部分表示要在项目之间放置的内容。它可以是任何字符串,甚至可以是“”(空字符串)这将产生ABCD而不是A、B、C、D非常感谢您的帮助回答!如果我使用第二个选项,是否有一种方法可以将所有输入的分数(A、B、C、D)相加?是的,类似于打印(“,”。加入(分数))(由于我在手机上未经测试,但应该没有打字错误)的“,“part表示您要在项目之间放置的内容。它可以是任何字符串,甚至可以是”“(空字符串),它将生成ABCD而不是A、B、C、D。非常感谢您的帮助!是否有方法将输入的分数(A、B、C)相加?如果将字母加在一起,你的意思是在一行@LilyHebert打印所有字母?如果是,请填写我的Uofat答案非常感谢你的帮助!有没有办法将输入的分数(a、B、C)加在一起?如果将字母加在一起,你的意思是在一行@LilyHebert打印所有字母?如果是,请填写我的Uofat答案