-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #608 from realpython/input-output-update
Add code examples for Basic I/O Update
- Loading branch information
Showing
5 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Basic Input and Output in Python | ||
|
||
This folder contains the code examples for the Real Python tutorial [Basic Input and Output in Python](https://realpython.com/python-input-output/). | ||
|
||
You can run all of the scripts directly by specifying their name: | ||
|
||
```sh | ||
$ python <filename>.py | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import random | ||
|
||
health = 5 | ||
enemy_health = 3 | ||
|
||
while health > 0 and enemy_health > 0: | ||
if input("Attack or Run? ").lower() == "attack": | ||
enemy_health -= 1 | ||
print("You hit the enemy!") | ||
# Implement a 50% chance that the enemy strikes back. | ||
enemy_attacks = random.choice([True, False]) | ||
if enemy_attacks: | ||
health -= 2 | ||
print("The enemy strikes back!") | ||
else: | ||
print("You ran away!") | ||
break | ||
print(f"Your health: {health}, Enemy health: {enemy_health}") | ||
|
||
print("Victory!" if enemy_health <= 0 else "Game Over") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
name = input("Please enter your name: ") | ||
print("Hello", name, "and welcome!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import random | ||
|
||
number = random.randint(1, 10) | ||
guess = int(input("Guess a number between 1 and 10: ")) | ||
|
||
if guess == number: | ||
print("You got it!") | ||
else: | ||
print(f"Sorry, the number was {number}.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import readline # noqa F401 | ||
|
||
while (user_input := input("> ")).lower() != "exit": | ||
print("You entered:", user_input) |