Question

There are 3 types of iteration constructs. What ones did you use in your program, and...

  • 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

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
3.plese to write the correct code: The code below will loop as long as the number...
3.plese to write the correct code: The code below will loop as long as the number that you enter isn’t negative. It will add each non negative number to the current sum. It will print out the sum and average of all of the numbers you have entered. But someone jumbled up the code. Can you reorganize and indent it so it works properly? count = count + 1 message = "Enter an integer or a negative number to stop"...
##4. What will the following program display? ##def main(): ## x = 1 ## y =...
##4. What will the following program display? ##def main(): ## x = 1 ## y = 3.4 ## print(x, y) ## first printing ## change_us(x, y) ## print(x, y) ##second printing ## ##def change_us(a, b): ## a = 0 ## b = 0 ## print(a, b) ## ##main() ## ##Yes, yes, main() displays ##1 3.4 ##0 0 ##1 3.4 ## The question is: why x and y are still the same while the second printing of (x,y)? ## It seems...
convert this code to accept int value instead of float values using python. Make sure to...
convert this code to accept int value instead of float values using python. Make sure to follow the same code. do not change the steps and make sure to point to what code you replaced. make sure to have 2 files Method:----------------------- #define a python user difined method def get_float_val (prompt): is_num = False str_val = input (prompt) #prming read for our while #while is_num == False: (ignore this but it works) old school while not is_num: try: value =...
Draw a flowchart based on the Python code below: ch = -1 #Variable declared for taking...
Draw a flowchart based on the Python code below: ch = -1 #Variable declared for taking option entered by the user print("Enter 0 to 5 for following options: ") print("0-> Issue new ticket number.") print("1-> Assign first ticket in queue to counter 1.") print("2-> Assign first ticket in queue to counter 2.") print("3-> Assign first ticket in queue to counter 3.") print("4-> Assign first ticket in queue to counter 4.") print("5-> Quit Program.") tickets = [] #Declaring list to store...
Write a program that asks the user to type in ages. They will type a negative...
Write a program that asks the user to type in ages. They will type a negative age when they finish entering the ages. The program will print out the average of all of the ages and the oldest age entered. It should end with a newline. Sample output #1 Type a negative for age to exit Enter your age: 21 Enter your age: 22 Enter your age: 21 Enter your age: -8 The average age is: 21.33 The oldest person...
Program will allow anywhere between 1 and 6 players (inclusive). Here is what your output will...
Program will allow anywhere between 1 and 6 players (inclusive). Here is what your output will look like: Enter number of players: 2 Player 1: 7S 5D - 12 points Player 2: 4H JC - 14 points Dealer: 10D Player 1, do you want to hit? [y / n]: y Player 1: 7S 5D 8H - 20 points Player 1, do you want to hit? [y / n]: n Player 2, do you want to hit? [y / n]: y...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics relating to data supplied by the user. The program will ask the user to enter the number of items making up the data. It will then ask the user to enter data items one by one. It will store the data items in a double array. Then it will perform a number of statistical operations on the data. Finally, it will display a report...
C++ PROGRAM SPECIFICATION For the assignment, we will use the previous assignment’s program that determines whether...
C++ PROGRAM SPECIFICATION For the assignment, we will use the previous assignment’s program that determines whether a college class room is in violation of fire law regulations regarding the maximum room capacity and add more logic to that program. We will need to make the following enhancements… The program should now determine the number of classes, and should do so by generating a unique, random number. Replace taking user input for the number of rooms with a computer generated number...
Consider the following research study: you wish to determine whether or not climate change will impact...
Consider the following research study: you wish to determine whether or not climate change will impact the feeding behavior of vampire bats. You divide a sample of bats into two groups and keep them in cages, each bat in its own individual cage. The first group is kept at a temperature of 30°C, which (we'll pretend) is about average for their natural habitat. The second group is kept in cages in a lab next door at 33°C. All of the...
Section 2: Using the MARS or SPIM simulator develop a program that will implement the following...
Section 2: Using the MARS or SPIM simulator develop a program that will implement the following conditional statement. If ( n is even) { n = n / 2; } else { n = 3 * n + 1; } In this case, n is to be input by the user (assume they input a non-negative value), the conditional is performed, and the resulting n is to be output. Again, use the system calls for input, output, and exiting the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT