Question

This program is a bit of a pain. It has to find the average tempearatures. If it is below 60, it must count how many numbers (that were averaged out) were below 60. It must do the same for any number above 80 if the average temperature is above 80. EDIT: instead of asking the user for the # of values, the program will keep accepting until given a blank value. This is my program:

def main():

    sums = 0.0
    count = 0
    heating = 0
    cooling = 0
    temp = (input("enter a number, <enter> to quit: "))
    while temp != " ":
        x = float(temp)
        sums = sums + x
        count = count + 1
        temp = eval(input("enter a number, <enter> to quit: "))
        avg = sums/count
        if avg < 60:
            if temp < 60:
                heating = heating + 1
        if avg > 80:
            if temp > 80:
                cooling = cooling + 1

    print(avg, heating, cooling)

main()

This is the error I keep getting. I have tried variations of asking for the input with and without the eval, and also with switching the temp between float and int. I keep getting errors, more commonly, this one.

Traceback (most recent call last):
  File "C:/Python33/heatingcooling.py", line 24, in <module>
    main()
  File "C:/Python33/heatingcooling.py", line 13, in main
    temp = eval(input("enter a number, <enter> to quit: "))
  File "<string>", line 0

   ^
SyntaxError: unexpected EOF while parsing

Any ideas on how to get this program to run would be much appreciated. As a side note, we are not allowed to use raw inputs, which seemed to be the most common solution for this

Was it helpful?

Solution

Your current loop confuses temp with x a few places. You need to be consistent about them because they're different types (temp is a string and x is a float). Here's a fixed version of the code, with a few other fixes, like testing for an empty string and providing an initial value for avg:

sums = 0.0
count = 0
heating = 0
cooling = 0
avg = 0      # need an initial value for this, in case the first input is empty!
temp = input("enter a number, <enter> to quit: ") # temp is always a string
while temp:      # equivalent to testing temp != ""
    x = float(temp)
    sums = sums + x
    count = count + 1
    avg = sums/count
    if avg < 60:
        if x < 60:          # compare to x here, not temp
            heating = heating + 1
    if avg > 80:
        if x > 80:          # here too
            cooling = cooling + 1

    temp = input("enter a number, <enter> to quit: ") # moved this to the end of the loop

print(avg, heating, cooling)

OTHER TIPS

Assuming you're in Python 3.x, removing the eval() should work with the input(...)

temp = input("enter a number, <enter> to quit: ")

The Python 3 documentation for input can be found here

Edit It sounds like you need to convert your temp variable into an int after you get it from the input() method, which is a str by default.

temp = float(input("enter a number, <enter> to quit: "))

Also, when you get your first temp variable, remove the outside parentheses:

temp = (input("enter a number, <enter> to quit: "))

to

temp = input("enter a number, <enter> to quit: ")

Assuming your enter a numeric value, you should then be able to cast this value to a float once inside the while loop.

Edit2

In your while loop change it to this:

while temp:  # or while temp != ""
    # etc.

This will resolve the ValueError: could not convert string to float error. You were terminating the while loop upon entering a space " ", but when the user hits enter, the input is really an empty string "". Thus, you were still entering the while loop and then trying to cast the empty string "" input into a float, which then raised the ValueError.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top