Press Enter to exit While Loop in Python 3.4

11,248

Solution 1

Try this corrected version of your code. Your logic is correct, but you had a few errors. You don't need eval, you had to convert the number to an integer when adding it to the total, and finally you had to define average outside of the function before you printed it out.

total = 0
count = 0
average = 0
data = input("Enter a number or press enter to quit: ")

while data:
    count += 1
    number = data
    total += int(number)
    average = total / count
    data = input("Enter a number or press enter to quit: ")

print("The sum is {0}. The average is {1}.".format(total, average))

Examples:

Enter a number or press enter to quit: 5
Enter a number or press enter to quit: 4
Enter a number or press enter to quit: 3
Enter a number or press enter to quit: 2
Enter a number or press enter to quit: 
The sum is 14. The average is 3.5.

Enter a number or press enter to quit: 
The sum is 0. The average is 0.

Solution 2

Keep the user's input as a string until you check its contents:

total = 0
count = 0

while 1:
    data = input("Enter a number or press enter to quit: ")
    try:
        data = float(data)
    except ValueError:
        break
    count += 1
    total += data

average = total / count
print("The sum is " + total ". The average is " + average + ".")

Solution 3

i separate a eval funtion isNumber I make it to keep floating decimal and it seems a bit cleaner.

def isNumber(value):
    try:
        float(value)
        return True
    except ValueError:
        "error"
        return False

total = 0
count = 0
data = input("Enter a number or press enter to quit: ")

while data and isNumber(data):
    count += 1
    number = float(data)
    total += number

average = total / count  #This sentences is more clean here (for me)
data = input("Enter a number or press enter to quit: ")
print("The sum is", total, ". ", "The average is", average)
Share:
11,248
Lprox5
Author by

Lprox5

Updated on June 15, 2022

Comments

  • Lprox5
    Lprox5 almost 2 years

    I am new to Python and have been teaching myself over the past few months. The book I am using teaches Python 2.7, while I am trying to learn Python in 3.4. I've become accustomed to using both now, but for the life of me I can't figure out how to exit this while loop with the enter key. The code appears below:

    total = 0
    count = 0
    data = eval(input("Enter a number or press enter to quit: "))
    
    while data != "":
        count += 1
        number = data
        total += number
        average = total / count
        data = eval(input("Enter a number or press enter to quit: "))
    print("The sum is", total, ". ", "The average is", average)
    

    I keep getting this error:

    Traceback (most recent call last):
      File "/Users/Tay/Documents/Count & Average.py", line 10, in <module>
        data = eval(input("Enter a number or press enter to quit: "))
      File "<string>", line 0
    
        ^
    SyntaxError: unexpected EOF while parsing
    

    I am able to get a modified version of this code to work in 2.7, but I would like to know how to do this in 3.4. I've searched around everywhere and can't seem to find an answer.

  • TigerhawkT3
    TigerhawkT3 almost 9 years
    By the way, while data != "": can be shortened to while data:.
  • Lprox5
    Lprox5 almost 9 years
    Thanks a bunch Henry! This piece of code makes much more sense to me at the level I'm at. I'm using Lambert's "Fundamentals of Python" and I'm only at the end of Chapter 3, so I haven't learned a lot of other functions.
  • TigerhawkT3
    TigerhawkT3 almost 9 years
    You can also remove average = total / count - no need to calculate it repeatedly - and replace the format() string's average with total/count.
  • Adarsha
    Adarsha almost 9 years
    This will probably end the loop with any non-numeric, right?