Search code examples
pythonfor-loopwhile-loopfor-in-loop

"while" not working inside "for" (completely ignored)


Basically, when I enter the temperature and if the value was more 60 and less than 0, it should say that the code is invalid and please type it again, until it gets a value between 1 and 59. But for my code, the while loop is like completely ignored... Why is that?

# TASK 1:
midDayTemp = float()
midNightTemp = float()
midDayList = []
midNightList = []

for day in range (3):
    print ("Day:", day + 1)
    midDayTemp = float(input("Please enter your Mid-Day temperature\n"))


    while midDayTemp >= 60 and midDayTemp <= 0:
         midDayTemp = input("Invalid, please re-enter your temperature\n")

    midDayList.append(midDayTemp)

    midNightTemp = float(input("Please enter your Mid-Night temperature\n"))


    while midNightTemp >= 60 and midNightTemp <= 0:
         midNightTemp = input("Invalid, please re-enter your temperature\n")

    midNightList.append(midNightTemp)
# TASK 2:
print("Mid-Day readings are ",midDayList)
midDayAverage = sum(midDayList) / len(midDayList)
print ("The average is ", midDayAverage)
print("Mid-Night readings are ",midNightList)
midNightAverage = sum(midNightList) / len(midNightList)
print ("The average is ", midNightAverage)


# TASK 3:
minValueMidDay = min(midDayList)
print ("The lowest temperature of the Mid-Day is", minValueMidDay )
maxValueMidDay = max(midDayList)
print ("The highest temperature of the Mid-Day is", maxValueMidDay )
minValueMidNight = min(midNightList)
print ("The lowest temperature of the Mid-Night is", minValueMidNight )
maxValueMidNight = max(midNightList)
print ("The highest temperature of the Mid-Night is", maxValueMidNight )

print ("Thank you for using my code!")

Thank you


Solution

  • That is because there is no number that is both greater than or equal to 60 and smaller or equal to zero.

    You must change the and for a or.

    while midDayTemp >= 60 or midDayTemp <= 0: