There are 3 types of iteration constructs. What ones did you use in your program, and why?
NUMS=4 i=1 lowMean=0 dayLow=0 dayHigh=0 highMean=0 highTemp=0 lowTemp=41 #Decalred required vriables print("--== Python Temperature Analyzer ==--") while(i<=4): high=int(input("Enter the high value of day"+str(i)+":")) low=int(input("Enter the low value of day"+str(i)+":")) #Reading high and low while(high>40 or high<-40 or low>40 or low<-40 or low>high): #if the conditons fails we again read the input print("Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low.") high=int(input("Enter the high value of day"+str(i)+":")) low=int(input("Enter the low value of day"+str(i)+":")) if(highTemp<high): #storing the high temparature highTemp=high dayHigh=i if(lowTemp>low): #storing the low temparature lowTemp=low dayLow=i lowMean+=low highMean+=high #adding hte high and low temparatures to highMean and lowMean i+=1 print("The average (mean) LOW temperature was:{:.2f}".format(lowMean/4)) #printing the mean of the low temparaature print("The average (mean) HIGH temperature was:{:.2f}".format(highMean/4)) #printing the mean of the high temparaature print("The average (mean) temparture was:{:.2f}".format((highMean/4+lowMean/4)/2)) #printing the mean of the temparature print("The highest temparature was "+str(highTemp)+" on day " +str(dayHigh)) #printing the high temparature and its day print("The lowest temparature was "+str(lowTemp)+" on day " +str(dayLow)) #printing the low temparature and its day
while loop is used in the program.
Because while loop runs until it's condition becomes false.
For the program , this property for while loop is necessary
1) while loop is used because it needs only condition statement but for - for loop it need membership operator and sequence to iterate. for loop can be used by calling range function. so that it will iterates through sequence of numbers.
2) Inner while loop takes input until user enters valid temperatures for the day that mean if user enters invalid temperatures the request again to input a valid temperatures. this while loop is necessary for input data errror checking and it's the only one way to do.
Get Answers For Free
Most questions answered within 1 hours.