If statements

Computer programs do not only execute instructions. Occasionally, a choice needs to be made. Such as a choice is based on a condition. Python has several conditional operators:

>   greater than
<   smaller than
==  equals
!=  is not

Conditions are always combined with variables. A program can make a choice using the if keyword. For example:

x = int(input("Tell X"))

if x == 4:
    print('You guessed correctly!')

print('End of program.')

When you execute this program it will always print ‘End of program’, but the text ‘You guessed correctly!’ will only be printed if the variable x equals to four (see table above). Python can also execute a block of code if x does not equal to 4. The else keyword is used for that.

x = int(input("Tell X"))

if x == 4:
    print('You guessed correctly!')
else:
    print('Wrong guess')

print('End of program.')

See the demonstration below:

if-statement

If x is set to 2, the second code block is executed. If x equals (==) four, the first code block is executed.


Text input and output
Variables

16 thoughts on “If statements

  1. Reply
    Sagar Dasgupta - April 21, 2016

    After writing the word else and typing the colon it is saying invalid syntax. I need help with this.

    1. Reply
      admin - April 26, 2016

      Make sure to have four spaces with every indent.

  2. Reply
    Alexander Horton - April 27, 2016

    What program exactly should I be using for this? I am using the Python 3.5.1 Shell on Mac and when I attempt to do the beginning of the program (x = int(input(“tell x”))) and hit enter a blue “Tell X” appears and if I attempt to go down another line to continue the program it treats it as if I have ended that program. This is probably an easy fix but any help is appreciated 🙂

    1. Reply
      admin - July 22, 2016

      Put the code in a text file, save it as example.py, then run with ‘python example.py’

  3. Reply
    user - June 10, 2016

    I have also not obtained, and spaces set, the combination of money changers, execution begins after the Enter.

    1. Reply
      admin - July 22, 2016

      Save your code as a file (create in a text editor), then run with ‘python program.py’

  4. Reply
    abdulmajeed - July 1, 2016

    when i type x = int(input(“Tell X”)) in the second line it says tell x, the what should i do to go to the next line without is saying syntaxerorr: invalid syntax

    1. Reply
      admin - July 22, 2016

      put all code in a file (text editor) and save as example.py. Then run with ‘python example.py’

  5. Reply
    abdulmajeed - July 1, 2016

    after typing x = int(input(“tell x”)) in the second line it says tell x , at the same line i write if x == 4 and then it says Traceback (most recent call last):
    File “”, line 1, in
    File “”, line 1
    if x == 4:
    ^
    SyntaxError: invalid syntax
    and if i press enter its the same , how should i go to the third line without this appearing

    1. Reply
      admin - July 22, 2016

      Use a text editor or Python IDE to create Python files. Save the file as example.py then run using ‘python example.py’

  6. Reply
    Anthony Newton - July 17, 2016

    Hi, I need help with this. For example, the:

    x = int(input(“Tell X”))

    if x == 4:
    print(‘You guessed correctly!’)
    else:
    print(‘Wrong guess’)

    print(‘End of program.’)

    One that it gives as an example, I want to know how to run it all at once in Python. When I type it out, after I type the:

    if x == 4:
    print(‘You guessed correctly!’)
    else:
    print(‘Wrong guess’)

    Bit out it already prints either “You guessed correctly” or “Wrong guess” but I want them to print after I have typed all the code. And when I copy and paste it all in at once, it doesn’t recognise another line/bit of it as meant to be on a new line and it doesn’t put the >>> before each bit therefore not running it properly.

    Am I missing something?

    1. Reply
      admin - July 22, 2016

      Execute the program as a file (Copy all code and save it as example.py), then run with: python example.py

  7. Reply
    emeka - July 17, 2016

    after writing print( ‘you guessed correctly!’) i get this error message indentation error ; expected an intend block please help , when i go back and click enter it works normal please assist

    1. Reply
      admin - July 22, 2016

      indention errors are about the number of spaces. Put four spaces inside the if statement blocks

  8. Reply
    jeykiran - July 26, 2016

    Where do i type in this command
    x = int(input(“Tell X”))

    if x == 4:
    print(‘You guessed correctly!’)
    else:
    print(‘Wrong guess’)

    print(‘End of program.’)
    i tried to type this command in python 3.5.2 Shell, its showing an error. i need help in this.

    Thanks

    1. Reply
      admin - July 26, 2016

      Copy this code into a text file, save it as example1.py
      Then run “python3 example1.py”

Leave a Reply

Your email address will not be published. Required fields are marked *