2. The parts of your program

2.1. Guessing numbers

2.2. The parts of the program

System Message: WARNING/2 (/home/runner/work/PyZombis/PyZombis/_sources/lectures/TWP30/TWP30_2_en.rst, line 29)

Title underline too short.

The parts of the program
-----------------------

Now let’s analyze the parts of the previous code

print("Welcome!")
g = input("Enter a number: ")
number = int(g)
if number == 42:
    print("You won!")
else:
    print("You lost!")
print("End of the game!")

Note that in the following program:

  • The parts in blue are functions (ex: print())

  • The parts in red are strings (ex: “Welcome!”)

  • The parts in green are numbers (ex: 42)

  • The parts in purple are directives (ex: if and else)

  • The parts in yellow are indentations (“Each in their own block”)

  • The parts in black are variables (ex: g and number)

  • The equal sign (=) is the assignment operator and is used to assign values to variables (ex: number = int(g) The variable ‘number’ receives an integer from g))

  • The double equal sign (==) is the comparison operator and is used to compare two variables or values (ex: number == 42 Is the number equal to 42?)

  • The colon (:) is the operator that opens an indentation block. It goes after the directives (ex: if number == 42: and else:)

Note: These are the colors of the code blocks that you have seen during the course, but these colors may vary depending on the programming tool used.

System Message: ERROR/3 (/home/runner/work/PyZombis/PyZombis/_sources/lectures/TWP30/TWP30_2_en.rst, line 75)

Duplicate ID – see lectures/TWP30/TWP30_2, line 79

.. parsonsprob:: ac_l30_2a
    :adaptive:
    :numbered: left

    Build the previous code using this drag and drop exercise. (Use the hint function if you get stuck)
    -----
    print("Welcome!")
    g = input("Enter a number: ")
    number = int(g)
    if number == 42:
       print("You won!")
    else:
       print("You lost!")
    print("End of the game!")
You have attempted of activities on this page