2. The parts of your program¶
2.1. Guessing numbers¶
2.2. 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:
andelse:
)
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.
Build the previous code using this drag and drop exercise. (Use the hint function if you get stuck)