average = total / count
while PEnd !="Q" :
total = total value
average = 0.0
value = int(input("Enter an input value: "))
total = total value
count = 1
value = int(input("Enter an input value: "))
PEnd = input("Enter character Q to quit or any other character to continue:
")
PEnd = " "
PEnd = input("Enter character Q to quit or any other character to continue: ")
total = 0
count = count 1
print("The average is: ", average)
Anyone know how to rearrange this code so that when the values that are inputted are 3 4 8 4 , the average is 4.75. have tried rewriting it and getting average as 2 with code below and 4 when count = 0
here's my try:
PEnd = " "
while PEnd !="Q" :
value = int(input("Enter an input value: "))
average = 0.0
total = 0
count = 1
total = total value
count = count 1
average = total / count
PEnd = input("Enter character Q to quit or any other character to continue: ")
print("The average is: ", average)
CodePudding user response:
break your program into its parts
- get a bunch of integers from the user
- average those numbers
- print the result
It helps me to actually break my program into its logical components so i would start with just getting a list of numbers
import re
def get_ints():
while True:
user_input = input("enter a number or numbers(or nothing to quit)")
if not user_input:
break
for a_number in re.findall("\d ",user_input):
yield int(a_number)
a_bunch_of_numbers = list(get_ints())
# you could enter 1 number at a time or a list of numbers seperated by non-numbers
print(a_bunch_of_numbers) # looks right :)
assuming(and verifying) that part works i move on to the next part of creating the average
average = sum(a_bunch_of_numbers)/len(a_bunch_of_numbers)
# or numpy.mean(a_bunch_of_numbers)
finally all i need to do is print the result
CodePudding user response:
Try initializing the variables before you start your while loop (see below).
PEnd = " "
count = 0
total = 0
while PEnd !="Q" :
value = int(input("Enter an input value: "))
total = value
count = 1
PEnd = input("Enter character Q to quit or any other character to continue: ")
average = total / count
print("The average is: ", average)