【问题标题】:Grade Average Calculator平均成绩计算器
【发布时间】:2015-05-31 04:24:03
【问题描述】:

这是我正在研究的问题:

您想知道自己在计算机科学方面的成绩,所以编写一个程序 连续将 0 到 100 之间的成绩带到标准输入 直到你输入 "stop" ,此时它应该打印你的平均值 到标准输出。

这是我目前在 Python 3 中的代码:

total = 0
q = 1
score = input("Enter a score:")
while score != "stop":
    q += 1
    total = total + int(score)
avg = total / q
print(avg)

我在编码方面非常陌生,可以使用一些帮助来为我指明正确的方向。我觉得学习对我很重要,所以没有人应该觉得有义务只给我正确的答案。

【问题讨论】:

  • 将您的输入读入列表
  • 不应该q从0开始吗?
  • 另外,循环中不需要另一个input 吗?

标签: python python-3.x while-loop


【解决方案1】:

看起来您可能会进入无限循环。您的 while 语句正在等待分数更改,但您不会在循环内修改分数。你应该添加

score = input("Enter a score:")

在你的 while 循环内。

【讨论】:

    【解决方案2】:
    total = 0
    total_quiz = 0
    inpt = input("Stop? or enter score")
    while inpt.upper() != "STOP":
        if int(inpt) < 100 and int(inpt) > 0:
            total += int(inpt)
            total_quiz += 1
        else:
            print("Score to high or to low")
        inpt = input("Stop? or enter score")
    print(total / total_quiz)
    

    既然是新手,你也应该知道正确的缩进在 python 中非常重要。

    【讨论】:

      【解决方案3】:

      我会给你一个完整的大纲,以帮助你在算法上完成这个过程。

      • 总变量为 0,计数变量为 0,并使用空输入字符串。
      • 创建一个 while 循环,直到输入变量是单词 stop。
      • 将输入作为整数添加到总数中。在计数中加一。再次输入。
      • 循环外除以浮点数除以累积计数。

      现在让我们尝试将这个算法放在代码中。像我在下面做的那样做。

      # Code Block 1
      
      count = 0  # count variable
      total = 0  # total variable
      
      enter = '' # input variable
      
      while enter != 'stop':
          enter = input('Enter a grade:' )
      
          if enter != 'stop' and enter.isdigit():
              total += int(enter) # add to total value
              count = count + 1   # then to the count
      
      print float(total) / count
      
      
      
      
      # Code Block 2
      
      numbers = []
      
      while enter != 'stop':
          enter = input('Enter a grade:' )
      
          if enter != 'stop':
              numbers.append(int(enter))
      
      print sum(numbers) / float(len(numbers))
      

      【讨论】:

      • 大家对我的问题都很有帮助,谢谢!不幸的是,MyProgramming Lab 不接受任何具有以下内容的内容:计数、+ 号、任何 enter.isdigit 或其变体。我了解到它寻找一种非常具体的编码方式。
      • 然后用一个列表,看看新的代码块。
      【解决方案4】:

      你的 while 循环没有缩进,但我不知道这是否是一个错字 - 你的主要问题是你需要一个 if 语句并且你的输入语句需要在 while 循环中,这是一个非常简单的程序只是改变你的代码(所以你应该理解它):

      total = 0
      q = 0 #need 0 so counter is correct
      score = "" #need to declare score
      
      print("Enter 'stop' to exit program") #Need to tell user how to quit
      while score != "stop": # you can use while score.lower() != stop to check all cases
          score = input("Enter a score:") 
          #You need to ask the question everytime so it should be in the while loop
          if score == "stop": #This is the part you were missing a conditional statement
              break           #if statment in this case that exits the loop when score is stop
          total += int(score) #you can use += here too
          q += 1 #moved from top so it doesn't add on the last run (when exited)
      
      avg = total / q
      print(avg)
      

      使用类似的 if 语句检查变量是否在 0 到 100 之间(提示搜索 else if)

      【讨论】:

        【解决方案5】:

        以下代码已被我的 Person 教科书接受。该问题指定了以下警告,但在其他方面似乎是同一个问题:“读取输入时,不要向用户显示提示。使用不带提示字符串的 input() 函数。”

        total = int()
        numOfGrades = int()
        grade = str()
        average = float()
        
        grade = input()
        
        while grade != "stop":
            numOfGrades = numOfGrades + 1
            total = total + int(grade)
            average = total/numOfGrades
            grade = input()
        print (average)
        

        【讨论】:

          猜你喜欢
          • 2019-08-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-03
          • 1970-01-01
          相关资源
          最近更新 更多