Created
October 25, 2017 03:27
-
-
Save MrValdez/2f5d89913250c17bdd1d2d4511541796 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#quiz6.py | |
# Make a program that stores a record of a person | |
# Ask the user if they want to print or change the record | |
# Use functions | |
# Name, Age, Gender | |
gender_choices = ["not given", "male", "female"] | |
record = {"Name": "Anonymous", | |
"Age": 0, | |
"Gender": gender_choices[0], | |
} | |
def print_choices(key): | |
key = key.lower() | |
if key == "age": | |
print("Age should be above 0 or below 120") | |
if key == "gender": | |
print("Gender should be any of the following:") | |
print(", ".join(gender_choices)) | |
def is_valid(key, value): | |
if key.lower() == "age": | |
if value.isnumeric(): | |
value = int(value) | |
if value > 0 and value < 120: | |
return True | |
if key.lower() == "gender": | |
return value in gender_choices | |
return False | |
def change_record(): | |
keys = ", ".join(record.keys()) | |
prompt = "Select from these keys: \n" + keys + "\n>>> " | |
selected_key = input(prompt) | |
key_found = None | |
for key in record.keys(): | |
if selected_key.upper() == key.upper(): | |
key_found = key | |
break | |
if key_found is None: | |
print(selected_key + " not found") | |
return | |
print_choices(key_found) | |
new_value = input("What's the new value? ") | |
if is_valid(key_found, new_value): | |
record[key_found] = new_value | |
else: | |
print("Value is invalid") | |
def view_record(): | |
for key, value in record.items(): | |
print("{}: {}".format(key, value)) | |
print("") | |
def menu(): | |
print("""1. Change record | |
2. View record | |
X. Exit""") | |
choice = input(">>> ").upper() | |
print("") | |
if choice in ["1", "2", "X"]: | |
if choice == "1": | |
change_record() | |
elif choice == "2": | |
view_record() | |
elif choice == "X": | |
return | |
menu() | |
if __name__ == "__main__": | |
menu() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment