From 5acbc247738b8abfc04f6f67d02e5a39979aa517 Mon Sep 17 00:00:00 2001 From: martin-martin Date: Sun, 3 Nov 2024 17:26:56 +0100 Subject: [PATCH 1/3] Add code examples for Basic I/O Update --- basic-input-output-in-python/README.md | 12 +++++++++++ .../adventure_game.py | 20 +++++++++++++++++++ basic-input-output-in-python/greeter.py | 2 ++ .../guess_the_number.py | 9 +++++++++ .../improved_input.py | 4 ++++ 5 files changed, 47 insertions(+) create mode 100644 basic-input-output-in-python/README.md create mode 100644 basic-input-output-in-python/adventure_game.py create mode 100644 basic-input-output-in-python/greeter.py create mode 100644 basic-input-output-in-python/guess_the_number.py create mode 100644 basic-input-output-in-python/improved_input.py diff --git a/basic-input-output-in-python/README.md b/basic-input-output-in-python/README.md new file mode 100644 index 0000000000..cc62191aec --- /dev/null +++ b/basic-input-output-in-python/README.md @@ -0,0 +1,12 @@ +# Basic Input and Output in Python + +This folder contains the code examples from the Real Python tutorial on [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 .py +``` + +If you're on a Windows sytem, then you'll need to install `pyreadline3` before you can see the benefits of importing `readline`, like done in `improved_input.py`. + diff --git a/basic-input-output-in-python/adventure_game.py b/basic-input-output-in-python/adventure_game.py new file mode 100644 index 0000000000..e634754a35 --- /dev/null +++ b/basic-input-output-in-python/adventure_game.py @@ -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") diff --git a/basic-input-output-in-python/greeter.py b/basic-input-output-in-python/greeter.py new file mode 100644 index 0000000000..8e515339d0 --- /dev/null +++ b/basic-input-output-in-python/greeter.py @@ -0,0 +1,2 @@ +name = input("Please enter your name: ") +print("Hello", name, "and welcome!") diff --git a/basic-input-output-in-python/guess_the_number.py b/basic-input-output-in-python/guess_the_number.py new file mode 100644 index 0000000000..3d485326ef --- /dev/null +++ b/basic-input-output-in-python/guess_the_number.py @@ -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}.") diff --git a/basic-input-output-in-python/improved_input.py b/basic-input-output-in-python/improved_input.py new file mode 100644 index 0000000000..3fff979660 --- /dev/null +++ b/basic-input-output-in-python/improved_input.py @@ -0,0 +1,4 @@ +import readline # noqa F401 + +while (user_input := input("> ")).lower() != "exit": + print("You entered:", user_input) From c09c5458efb0d8061800a497ce78e6ec728c7e1b Mon Sep 17 00:00:00 2001 From: brendaweles <160772586+brendaweles@users.noreply.github.com> Date: Fri, 15 Nov 2024 12:36:20 -0700 Subject: [PATCH 2/3] Update README.md --- basic-input-output-in-python/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basic-input-output-in-python/README.md b/basic-input-output-in-python/README.md index cc62191aec..e31d8806ed 100644 --- a/basic-input-output-in-python/README.md +++ b/basic-input-output-in-python/README.md @@ -1,6 +1,6 @@ # Basic Input and Output in Python -This folder contains the code examples from the Real Python tutorial on [Basic Input and Output in Python](https://realpython.com/python-input-output/). +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: From 285edff55b2ebdb3935e7f47e3aa50b269c3172b Mon Sep 17 00:00:00 2001 From: brendaweles <160772586+brendaweles@users.noreply.github.com> Date: Fri, 15 Nov 2024 14:32:57 -0700 Subject: [PATCH 3/3] Update README.md --- basic-input-output-in-python/README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/basic-input-output-in-python/README.md b/basic-input-output-in-python/README.md index e31d8806ed..f7282a1684 100644 --- a/basic-input-output-in-python/README.md +++ b/basic-input-output-in-python/README.md @@ -7,6 +7,3 @@ You can run all of the scripts directly by specifying their name: ```sh $ python .py ``` - -If you're on a Windows sytem, then you'll need to install `pyreadline3` before you can see the benefits of importing `readline`, like done in `improved_input.py`. -