Skip to content

Commit

Permalink
Merge branch 'master' into expressions-vs-statements
Browse files Browse the repository at this point in the history
  • Loading branch information
brendaweles authored Nov 19, 2024
2 parents a994b18 + a046f4e commit a68d9d7
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 6 deletions.
9 changes: 9 additions & 0 deletions basic-input-output-in-python/README.md
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
```
20 changes: 20 additions & 0 deletions basic-input-output-in-python/adventure_game.py
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")
2 changes: 2 additions & 0 deletions basic-input-output-in-python/greeter.py
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!")
9 changes: 9 additions & 0 deletions basic-input-output-in-python/guess_the_number.py
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}.")
4 changes: 4 additions & 0 deletions basic-input-output-in-python/improved_input.py
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)
4 changes: 2 additions & 2 deletions python-concurrency/cpu_processes.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import concurrent.futures
import time
from concurrent.futures import ProcessPoolExecutor


def main():
start_time = time.perf_counter()
with concurrent.futures.ProcessPoolExecutor() as executor:
with ProcessPoolExecutor() as executor:
executor.map(fib, [35] * 20)
duration = time.perf_counter() - start_time
print(f"Computed in {duration} seconds")
Expand Down
4 changes: 2 additions & 2 deletions python-concurrency/cpu_threads.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import concurrent.futures
import time
from concurrent.futures import ThreadPoolExecutor


def main():
start_time = time.perf_counter()
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
with ThreadPoolExecutor(max_workers=5) as executor:
executor.map(fib, [35] * 20)
duration = time.perf_counter() - start_time
print(f"Computed in {duration} seconds")
Expand Down
4 changes: 2 additions & 2 deletions python-concurrency/io_threads.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import concurrent.futures
import threading
import time
from concurrent.futures import ThreadPoolExecutor

import requests

Expand All @@ -19,7 +19,7 @@ def main():


def download_all_sites(sites):
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
with ThreadPoolExecutor(max_workers=5) as executor:
executor.map(download_site, sites)


Expand Down

0 comments on commit a68d9d7

Please sign in to comment.