Home > Back-end >  Zybooks 5.12 counting negative temperatures
Zybooks 5.12 counting negative temperatures

Time:10-23

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)  

CodePudding user response:

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.

  • Related