Grade Average Calculator
Solution 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))
Solution 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.
Solution 3:
The following code was accepted by my Person textbook. The question specified the following caveat, but otherwise appears to be the same question: "When reading the input, do not display a prompt for the user. Use the input()function with no prompt string."
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)
Solution 4:
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.
Solution 5:
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.
Post a Comment for "Grade Average Calculator"