Zybooks 5.12 counting negative temperatures

Question:

I’m trying to write a code that will take 6 integers and count how many are negative. It needs to be formatted like this:

Enter Celsius temperature:
30
Enter Celsius temperature:
20
Enter Celsius temperature:
-5
Enter Celsius temperature:
-18
Enter Celsius temperature:
-8
Enter Celsius temperature:
7
Number of below freezing temperatures: 3

Heres my code so far:

temp = int(input('Enter Celsius temperature:n'))
temp_list = []
negtemp = 0

for i in range(6):
    while temp < 0:
        negtemp += 1
        temp_list.append(temp)
        
    
print('Number of below freezing temperatures: ', negtemp)  
Asked By: Abby Kabalin

||

Answers:

You need to rewrite your code like this,

negtemp = 0
for _ in range(6):
    temp = int(input('Enter Celsius temperature:n'))
    if temp < 0:
        negtemp += 1
    
print('Number of below freezing temperatures: ', negtemp)  

You aren tried to use while loop instead of if condition.

Answered By: Rahul K P
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.