diff --git a/basic-input-output-in-python/README.md b/basic-input-output-in-python/README.md new file mode 100644 index 0000000000..f7282a1684 --- /dev/null +++ b/basic-input-output-in-python/README.md @@ -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 .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)