I want to learn python
Let's take a look at the learning path designed to build your Python skills from the ground up:
I just want to learn Python for fun and to make simple games.
Which repl should i use
print("Welcome to the Haunted Castle!")
print() function takes the message inside the double quotes and displays it on the black console screen below. Once you run this, you have officially written and executed your very first Python game statement.Continue: I ran it! How do we save the player's name in a variable?
=), and the value you want to assign to it. Let's create a variable called player_name and set it to a starting name like "Alex".player_name = "Alex"
print("Hello, " + player_name + "!")
player_name and assign it the text value "Alex". In programming, text characters wrapped in quotes are known as strings. In the second line, we use the + symbol to join, or , our text pieces together. The print() function then displays the combined message. Try typing those two lines into your REPL window, hit "Run", and see how Python greets the player by name.Continue: How do we let players type in their own custom names?
input() function to a variable. We can even pass a message inside the parentheses of input() to show a prompt, so players know exactly what they are supposed to type. This is how we keep our code incredibly clean while building a custom greeting.player_name = input("Enter your character's name: ")
print("Welcome to your adventure, " + player_name + "!")
Explain this command of Print, hello, plus player, name and plus !
+ operator. In programming, the + sign doesn't just add numbers; when placed between pieces of text, it acts as a digital glue, merging them end-to-end into one single message.| Code Piece | Type | What It Represents / Does |
|---|---|---|
"Hello, " | String (Static Text) | The opening greeting. Notice the space inside the quotes after the comma! |
+ | Operator | The glue that joins the preceding piece of text to the next piece. |
player_name | Variable | The storage box holding whatever custom name the player typed in. |
+ | Operator | The glue that joins the player's name to our final punctuation. |
"!" | String (Static Text) | The exclamation point wrapped in quotes to wrap up the sentence enthusiastically. |
"Hello, ". Python is very literal and will not automatically insert spaces for you. If we wrote "Hello," without that trailing space, the final joined output would look like Hello,Alex! instead of Hello, Alex!. Including the space inside our ensures the computer formats the sentence correctly for human eyes.If player_name currently stores "Sam", what will print("Good luck " + player_name + "!") display on the screen?
Good luckSam!
Good luckSam! rather than a formatted sentence. Spotting how literal the computer is with spaces is a major milestone in mastering game dialog.if keyword. This tells Python to run a block of code only if a specific condition is met. For example, if a player chooses to go left, we print one message; if they go right, we print another. Let's see how this looks in code:choice = input("Do you want to go left or right? ")
if choice == "left":
print("You walk into a dark room and find a treasure chest!")
== to check if two values are equal. Notice the on the second line. In Python, the four spaces at the start of the print line tell the computer that this statement belongs inside the if block, meaning it will only run if the player typed "left".What happens if the player types right instead?
if statement. Because the evaluating choice == "left" turns out to be False, Python simply skips over that print line and continues running any code that comes afterward. In our current script, since nothing else is written, the game would quietly end, leaving your player staring at a blank console screen.To prevent our game from ending abruptly and to make sure we react to other decisions, we can use an else statement. Think of else as the ultimate safety net: if the if condition turns out to be False, Python will jump straight to the else block and run whatever is inside it instead. This lets us handle alternative paths without writing a second check.
choice = input("Do you want to go left or right? ")
if choice == "left":
print("You walk into a dark room and find a treasure chest!")
else:
print("You step to the right and fall into a bottomless pit!")
else keyword has a colon after it and is lined up perfectly with the if statement, while the second print line is also indented by four spaces. If you run this code in your editor and type "right", the computer will skip the first chest and print the tragic news of the bottomless pit.