Average using loops

89
April 05, 2022, at 4:30 PM
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)
Answer 1

break your program into its parts

  1. get a bunch of integers from the user
  2. average those numbers
  3. 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

Answer 2

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)
Rent Charter Buses Company
READ ALSO
I think it's a basic question but I don't get why I get 0 in this function

I think it's a basic question but I don't get why I get 0 in this function

I'm wondering why I get 0 for the print(a) call in the last lineI put 0 for the first loop of the add()function and it should return 1 and so on in the for loop

118
Python variable won't update outside of module

Python variable won't update outside of module

I have a variable called store_number that I pass in from my mainpy into a module called store_selector which increments the store number until it finds a valid store

102
I can't install anything with pip [duplicate]

I can't install anything with pip [duplicate]

i tried installing numPy using this command

59