Grade Average Calculator

两盒软妹~` 提交于 2019-12-23 15:53:14

问题


Here is the question I am working on :

You want to know your grade in Computer Science, so write a program that continuously takes grades between 0 and 100 to standard input until you input "stop" , at which point it should print your average to standard output.

Here is my code thus far in 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)

I am very new at coding, and could use some help to point me in the right direction. I feel its important for me to learn, so nobody should feel obligated to just hand me the correct answer.


回答1:


I'll give you a thorough outline to help you with this process algorithmically.

  • Total variable to 0, count variable to 0, and use an empty input string.
  • Create a while loop that runs until the input variable is the word stop.
  • Add input as integer to total. Add one to the count. Take input again.
  • Outside of loop divide total as float divided by the cumulative count.

Now let's try putting this algorithm together in code. Do as I do down below.

# 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))



回答2:


It looks like you might be going into an infinite loop. Your while statement is waiting for score to change, but you don't modify score within the loop. You should add

score = input("Enter a score:")

inside of your while loop.




回答3:


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)

Since your new, you should also know correct indentations is very important in python.




回答4:


Your while loop is not indented but I don't know if this is a typo - your main problem is that you need an if statement and your input statement needs to be in the while loop, this is a very simple program that just alters your code (so you should understand it):

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)

Using a similar if statement to check if (hint search for else if) the variable is between 0 and 100 I leave to you but feel free to ask if you're still stuck.



来源:https://stackoverflow.com/questions/29290556/grade-average-calculator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!