Completed 10-11 Favorite Number
This commit is contained in:
parent
0420c90a99
commit
a4e50e9cb7
15
Chapter_10/10-11_favorite_number.py
Normal file
15
Chapter_10/10-11_favorite_number.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
# Exercise 10-11 Favorite Number
|
||||||
|
# Learning Objective: Save data in JSON format.
|
||||||
|
|
||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
path = Path("./Chapter_10/favorite_number.json")
|
||||||
|
|
||||||
|
favorite_number = input("What is your favorite number?")
|
||||||
|
|
||||||
|
path.write_text(json.dumps(favorite_number))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
12
Chapter_10/10-11b_favorite_number.py
Normal file
12
Chapter_10/10-11b_favorite_number.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
# Exercise 10-11b Favorite Number
|
||||||
|
# Learning Objective: Load stored JSON formatted data.
|
||||||
|
|
||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
path = Path("./Chapter_10/favorite_number.json")
|
||||||
|
|
||||||
|
contents = path.read_text()
|
||||||
|
favorite_number = json.loads(contents)
|
||||||
|
|
||||||
|
print(f"I know your favorite number. It's {favorite_number}.")
|
||||||
1
Chapter_10/favorite_number.json
Normal file
1
Chapter_10/favorite_number.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
"21343524"
|
||||||
Loading…
x
Reference in New Issue
Block a user