Completed 10-14 Verify User
This commit is contained in:
parent
cca2d5153d
commit
e01dcd792e
55
Chapter_10/10-14_verify_user.py
Normal file
55
Chapter_10/10-14_verify_user.py
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
# Exercise 10-14 Verify User
|
||||||
|
# Learning Objective: Modify existing code to add an additional step.
|
||||||
|
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
import json
|
||||||
|
|
||||||
|
def get_stored_info(path):
|
||||||
|
"""Get stored username if available."""
|
||||||
|
if path.exists():
|
||||||
|
contents = path.read_text()
|
||||||
|
information = json.loads(contents)
|
||||||
|
return information
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_information(path):
|
||||||
|
"""Prompt for information."""
|
||||||
|
data = {
|
||||||
|
|
||||||
|
}
|
||||||
|
username = input("What is your name? ")
|
||||||
|
location = input("What country/state are you in? ")
|
||||||
|
age = input("How old are you? ")
|
||||||
|
data["username"] = username
|
||||||
|
data["location"] = location
|
||||||
|
data["age"] = age
|
||||||
|
save_data = json.dumps(data)
|
||||||
|
path.write_text(save_data)
|
||||||
|
return data
|
||||||
|
|
||||||
|
def verify_user(information):
|
||||||
|
verification = input(f"Are you {information["username"]} (y/n)?")
|
||||||
|
if verification.lower() == 'n' or verification.lower() == 'no':
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
|
def greet_user():
|
||||||
|
"""Greet the user by name."""
|
||||||
|
path = Path('user_info.json')
|
||||||
|
information = get_stored_info(path)
|
||||||
|
if information:
|
||||||
|
confirmed = verify_user(information)
|
||||||
|
if confirmed == False:
|
||||||
|
entered_data = get_information(path)
|
||||||
|
elif confirmed == True:
|
||||||
|
print(f"Welcome back, {information["username"]}! We remember that you are from {information["location"]} and that you are {information["age"]} years old since the last time you ran this.")
|
||||||
|
else:
|
||||||
|
entered_data = get_information(path)
|
||||||
|
print(f"We'll remember this information for when you come back: \n")
|
||||||
|
for i in entered_data:
|
||||||
|
print(f"{entered_data[i]}")
|
||||||
|
|
||||||
|
greet_user()
|
||||||
@ -1 +1 @@
|
|||||||
{"username": "Steve", "location": "CZ", "age": "32"}
|
{"username": "Bregg", "location": "RT", "age": "833"}
|
||||||
Loading…
x
Reference in New Issue
Block a user