-1

WHy doesn't this code gives an error in python as the else part is un-indented and doesn't have a starting if block.

guess=7
c=1
for c in range(1,6):
  g = int(input("guess a number from 1 to 20:"))
  if (g==guess):
    print("you win")
    break 
else:
  print("you lose")

1 Answers1

0

That else is for the for loop. If the for loop is completed without a break, it is not implemented, if not the else block will be executed.

guess=7
c=1
for c in range(1,6):
  g = int(input("guess a number from 1 to 20:"))
  if (g==guess):
    print("you win")
    break 
else:  # This else is for "for loop"
  print("you lose")
Bibhav
  • 1,579
  • 1
  • 5
  • 18