else & elif statements not working in Python

后端 未结 9 841
礼貌的吻别
礼貌的吻别 2020-12-10 11:06

I\'m a newbie to Python and currently learning Control Flow commands like if, else, etc.

The if statement is working all fine

相关标签:
9条回答
  • 2020-12-10 11:46

    indentation is important in Python. Your if else statement should be within triple arrow (>>>), In Mac python IDLE version 3.7.4 elif statement doesn't comes with correct indentation when you go on next line you have to shift left to avoid syntax error.

    0 讨论(0)
  • 2020-12-10 11:49

    It looks like you are entering a blank line after the body of the if statement. This is a cue to the interactive compiler that you are done with the block entirely, so it is not expecting any elif/else blocks. Try entering the code exactly like this, and only hit enter once after each line:

    if guess == number:
        print('Congratulations! You guessed it.')
    elif guess < number:
        pass # Your code here
    else:
        pass # Your code here
    
    0 讨论(0)
  • 2020-12-10 11:49
     if guess == number:
         print ("Good")
     elif guess == 2:
         print ("Bad")
     else:
         print ("Also bad")
    

    Make sure you have your identation right. The syntax is ok.

    0 讨论(0)
提交回复
热议问题