Ask Question
3 April, 18:38

Retype the below code. Fix the indentation as necessary to make the program work. if 'New York' in temperatures: if temperatures['New York'] > 90: print ('The city is melting!') else: print ('The temperature in New York is', temperatures['New York']) else: print ('The temperature in New York is unknown.') Sample output with input: 105 The city is melting!

+5
Answers (1)
  1. 3 April, 20:20
    0
    Answer: Here is the indented code:

    if 'New York' in temperatures:

    if temperatures['New York'] > 90:

    print ('The city is melting!')

    else:

    print ('The temperature in New York is', temperatures['New York'])

    else:

    print ('The temperature in New York is unknown.')

    Explanation:

    Here is the complete program:

    temperatures = {

    'Seattle': 56.5,

    'New York': 105,

    'Kansas City': 81.9,

    'Los Angeles': 76.5

    }

    if 'New York' in temperatures:

    if temperatures['New York'] > 90:

    print ('The city is melting!')

    else:

    print ('The temperature in New York is', temperatures['New York'])

    else:

    print ('The temperature in New York is unknown.')

    The output is:

    The city is melting!

    The program basically has nested if statements. The first If statement checks if New York is present in temperatures list. If this is true then the program control enters another if condition which checks if the temperature value of New York is greater than 90. If this evaluates to true then message The city is melting! is displayed. If this evaluates to false then else part is executed which displays the temperature in New York. If none of the above if statements evaluate to true then the final else part is executed which displays The temperature in New York is unknown. Since the value of New York temperature is set to 105 so The city is melting! is displayed in output.

    Indentation in python is important as it does not uses indentation instead of using brackets to describe a block of statement. It makes the code readable. See the above nested if statements. The inner If statement is indented in such a way that it looks inside the outer if statement without the use of brackets. The else part of inner if is placed right below inner if and the else part of the outer if indented below the outer if statement. Also the print statement are indented with their corresponding if and else parts.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Retype the below code. Fix the indentation as necessary to make the program work. if 'New York' in temperatures: if temperatures['New ...” in 📙 Engineering if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers