Skip to content

Commit a68d9d7

Browse files
authored
Merge branch 'master' into expressions-vs-statements
2 parents a994b18 + a046f4e commit a68d9d7

File tree

8 files changed

+50
-6
lines changed

8 files changed

+50
-6
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Basic Input and Output in Python
2+
3+
This folder contains the code examples for the Real Python tutorial [Basic Input and Output in Python](https://realpython.com/python-input-output/).
4+
5+
You can run all of the scripts directly by specifying their name:
6+
7+
```sh
8+
$ python <filename>.py
9+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import random
2+
3+
health = 5
4+
enemy_health = 3
5+
6+
while health > 0 and enemy_health > 0:
7+
if input("Attack or Run? ").lower() == "attack":
8+
enemy_health -= 1
9+
print("You hit the enemy!")
10+
# Implement a 50% chance that the enemy strikes back.
11+
enemy_attacks = random.choice([True, False])
12+
if enemy_attacks:
13+
health -= 2
14+
print("The enemy strikes back!")
15+
else:
16+
print("You ran away!")
17+
break
18+
print(f"Your health: {health}, Enemy health: {enemy_health}")
19+
20+
print("Victory!" if enemy_health <= 0 else "Game Over")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name = input("Please enter your name: ")
2+
print("Hello", name, "and welcome!")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import random
2+
3+
number = random.randint(1, 10)
4+
guess = int(input("Guess a number between 1 and 10: "))
5+
6+
if guess == number:
7+
print("You got it!")
8+
else:
9+
print(f"Sorry, the number was {number}.")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import readline # noqa F401
2+
3+
while (user_input := input("> ")).lower() != "exit":
4+
print("You entered:", user_input)

python-concurrency/cpu_processes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import concurrent.futures
21
import time
2+
from concurrent.futures import ProcessPoolExecutor
33

44

55
def main():
66
start_time = time.perf_counter()
7-
with concurrent.futures.ProcessPoolExecutor() as executor:
7+
with ProcessPoolExecutor() as executor:
88
executor.map(fib, [35] * 20)
99
duration = time.perf_counter() - start_time
1010
print(f"Computed in {duration} seconds")

python-concurrency/cpu_threads.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import concurrent.futures
21
import time
2+
from concurrent.futures import ThreadPoolExecutor
33

44

55
def main():
66
start_time = time.perf_counter()
7-
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
7+
with ThreadPoolExecutor(max_workers=5) as executor:
88
executor.map(fib, [35] * 20)
99
duration = time.perf_counter() - start_time
1010
print(f"Computed in {duration} seconds")

python-concurrency/io_threads.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import concurrent.futures
21
import threading
32
import time
3+
from concurrent.futures import ThreadPoolExecutor
44

55
import requests
66

@@ -19,7 +19,7 @@ def main():
1919

2020

2121
def download_all_sites(sites):
22-
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
22+
with ThreadPoolExecutor(max_workers=5) as executor:
2323
executor.map(download_site, sites)
2424

2525

0 commit comments

Comments
 (0)