【问题标题】:How to record inputs when using "For Loop"使用“For Loop”时如何记录输入
【发布时间】:2019-05-04 03:31:21
【问题描述】:

我试图让用户输入他们有多少个课程 (x),问“你在这些课程中的成绩是多少?” x 次,并记录所有输入的成绩以供以后使用。

我试图将问题分配给一个变量并要求打印该变量,但我只得到最后输入的数字。我不想打印这些数字,我想将它们存储起来以备后用,以便将它们加在一起。我只是使用打印功能来查看如果分配变量实际有效,我的数字将如何存储。我如何记录所有输入的数字以供以后添加和计算 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 选项,我只能记录最后输入的数字。

【问题讨论】:

    标签: python for-loop


    【解决方案1】:

    你要使用的东西是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
    

    【讨论】:

    • 非常感谢您的帮助!有没有办法将输入的成绩(A、B、C)加在一起?
    • 如果将字母加在一起是指将它们全部打印在一行中@LilyHebert?如果是,请提供我的 uofated 答案
    【解决方案2】:

    在我看来,有几个选项可能是合适的。

    每次迭代打印输入:

    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"]
    

    【讨论】:

    • 非常感谢您的帮助!如果我使用第二个选项,有没有办法让我将所有输入的成绩(A、B、C、D)加在一起?
    • 是的,类似 print(“, ”.join(grades)) (因为我在移动设备上,所以未经测试,但应该没有错别字)“,”部分表示您想要放在中间的内容项目。它可以是任何字符串,甚至可以是生成 ABCD 而不是 A、B、C、D 的“”(空字符串)
    【解决方案3】:

    这是你的做法:

    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)
    

    上面应该可以做到。

    【讨论】:

      猜你喜欢
      • 2013-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多