Control Structures: Change the following code (...) to generate "else"

33 Views Asked by At

How can I change the following code to include an else statement?

if 2 + ... == 4 ...
  if 3 * ... == 9 ...
    print ("if")
  else: 
    print ("else")

This is what I have tried:

if 2 + 2 == 4 / 2
  if 3 * 3 == 9 / 3
    print ("if")
  else: 
    print ("else")

However, I am getting a syntax error:

File "", line 1 if 2 + 2 == 4 / 2 ^ SyntaxError: invalid syntax

1

There are 1 best solutions below

4
dodrg On

I don't think, it is useful to provide you a finished solution for your study task.

The if-statement line hast to end with a : (as the else does)

But perhaps you also have a look at your maths think about the else, to which 'if` it belongs to.


Edit: By your answers I've got the impression, you're not watching for the correct intents.

The program skeleton is:

if 2 + ... == 4 ...
  if 3 * ... == 9 ...
    print ("if")
  else: 
    print ("else")

But your answer seems to have changed the skeleton code this way:

if 2 + ... == 4 ...
  if 3 * ... == 9 ...
    print ("if")
else: 
    print ("else")

Please note the missing intent of the else in the second version! This little change changes the logic of the program and will prohibit a successful solution.