2

I'm so close to figuring this thing out, but I must be looking right past the solution. This program is meant to receive temperature values in Celsius, convert them to Fahrenheit, and then count the number of "cool", "warm", and "hot" days. I've got the first two parts of this problem figured out, but for whatever reason, my program isn't counting the number of each kind of day correctly.

temperature_list = []

value = int(input("Number of temperatures to enter: "))

for i in range(value):

    #We now collect the values

    t = int(input("Enter temperature: "))

    temperature_list.append(t)

print("Your entered temperatures in Celsius are: ", temperature_list)

#Next up is to print those temperatures in Fahrenheit, which we'll do in a batch
f_list = list()
f = [t*1.8 + 32 for t in temperature_list]
f_list.append(f)
fint = int(f[0])


cooldays = 0
hotdays = 0
for f in f_list:
    if fint > 65 :
        cooldays = cooldays + 1
    if fint < 80 :
        hotdays = hotdays + 1
print("Your temperature in Fahrenheit is: ", f[:])
warmdays = (len(f_list) - (cooldays + hotdays))
print(cooldays)
print(hotdays)
print(warmdays)

Can someone tell me what I'm missing, here?

7
  • You're using fint in your loop for your conditionals when you should probably use int(f) Commented Dec 9, 2020 at 5:12
  • 1
    You logic also allows both cooldays and hotdays to occur in the same day, making your warmdays not valid Commented Dec 9, 2020 at 5:13
  • I think you did the > and < in the wrong directions. You are checking if colds days are more that 65 and hot days are less than 80 which doesn't seem right to me. Commented Dec 9, 2020 at 5:13
  • You mentioned incorrect counts. Can you tell us the current output and the expected. Don't forget to tells us the input Commented Dec 9, 2020 at 5:20
  • Grand J, you're absolutely right. I redid the lines so often that I neglected to check which way the signs were facing! Replacing "fint" with "f" gave me the usual "'<' not supported between instances of 'list' and 'int'" error, though. Commented Dec 9, 2020 at 5:21

1 Answer 1

3

Currently, you are checking fint with the threshold value for every iteration. Instead you want to check it against each value in f_list.

Secondly, depending on your requirements, you also probably need to change your conditions. Like for example 66 is greater than 65 and also less than 80. So it will increment both... Please revise your conditions.

Third,f = [t*1.8 + 32 for t in temperature_list] makes f a list of floats. You append this to another list, f_list. Essentially, your f_list is now a 2D matrix. For example: [[65,66,68.2, 98.3]] Note the double square braces... You could just assign the temperatures directly to f_list, instead of creating an intermediate variable f

f_list = [t*1.8 + 32 for t in temperature_list]
for f in f_list:
    if f < 65 :
        cooldays = cooldays + 1
    elif f > 80 :
        hotdays = hotdays + 1
Sign up to request clarification or add additional context in comments.

12 Comments

How is that different to the question?
Ah nvm me I didn't immediately notice the int part being removed
Why did you change it to >= 65 and < 80? It was fine before as it is two ifs and not if elif. I think the previous one better matched the OPs question as we don't know what values are expected
I think we need the OPs opinion here for more clarity. Note that OP casts fint to an int.
This solved it! I needed to turn some of the signs around, but removing the 2D matrix and just using the values worked like a charm! Thank you so much.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.