“ while”不在“ for”内部工作(完全忽略)

时间:2019-01-15 16:20:24

标签: python for-loop while-loop for-in-loop

基本上,当我输入温度时,如果该值大于60且小于0,则应该说该代码无效,请再次键入它,直到得到介于1和59之间的值。但是对于我的代码,而while循环就像完全被忽略了……为什么?

# 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!")

谢谢

1 个答案:

答案 0 :(得分:2)

这是因为不存在大于或等于60且小于或等于零的数字。

您必须将and更改为or。

while midDayTemp >= 60 or midDayTemp <= 0: