programmer at Google·
Needs advice
on
PyCharmPyCharm
and
PythonPython

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?

READ LESS
4 upvotes·21.9K views
Replies (3)
sysadmin at Overpass·
Recommends
on
Python

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: {}"}, }

questions

for key, item in contact.items(): item["value"] = input(item["prompt"])

responses:

for key, item in contact.items(): print(item["response"].format(item["value"]))```

READ MORE
3 upvotes·18K views
DevOps/Automation Engineer ·
Recommends
on
Python

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}")
READ MORE
7 upvotes·1 comment·20.6K views
farsi bloger
farsi bloger
·
June 18th 2023 at 6:04AM

thank you for sharing

·
Reply
View all (3)
Avatar of Sergiu Neamt

Sergiu Neamt

sysadmin at Overpass