name = input("what is your name?") lastname = input("what is your last name") age = input("who old are you?") hoope = input("what your fafret hoope?") work = input("what your work?") country = input("wher you ate from?") print("NAME: " + name) print("Last Nmae" + lastname) print("Aeg: " + age) print("youre hoope: " + hoope) print("youre work is: " + work) print("youre counry is: " + country)
Is there a shortcut to the code?
how about: ```contact = { "name": {"value": None, "prompt":"What is your name? ", "response": "Name: {}"}, "lastname": {"value": None, "prompt": "What is your last name? ", "response": "Last Name {}"}, "age": { "value": None, "prompt": "How old are you? ", "response": "Age: "}, "hoope": { "value": None, "prompt": "What your fafret hoope? ", "response": "Your hoope: {}"}, "work": { "value": None, "prompt": "What your work? ", "response": "Your work is: {}"}, "country": { "value": None, "prompt": "Where are you from? ", "response": "Your country is: {}"}, }
questionsfor key, item in contact.items(): item["value"] = input(item["prompt"])
responses:for key, item in contact.items(): print(item["response"].format(item["value"]))```
use dictionary and loop to minimize repetition, questions are stored in a dictionary, and a loop is used to ask each question, responses are stored in another dictionary. Another loop prints out the responses. This approach is more scalable and easier to maintain, if you want to add more questions, you can just add them to the questions dictionary, and the rest of the code will handle them automatically. You could also store the questions in a text file or db and create the dict dynamically... Here's a simple example using dict and loops:
responses = {}
questions = {"name": "What is your name? ",
"lastname": "What is your last name? ",
"age": "How old are you? ",
"hobby": "What is your favorite hobby? ",
"work": "What is your work? ",
"country": "Where are you from? "}
for key, question in questions.items():
responses[key] = input(question)
for key, response in responses.items():
print(f"{key.capitalize()}: {response}")