0
with open("bankaccount.txt", 'a+') as f:
    if User1_working_status =="1":
        f.write("{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n".format("Name: "     + user1_title + " " + user1_name,        
        "Gender:" + user1_gender,"Town and country: "
        + user1_town, "Age: " + user1_age,"Country and town of birth: "+ user1_country_of_birth,"Nationality: "
        + user1_nationality,"Country residence:"+user1_country_residence,"Tax resident country: "
        + user1_tax_res,"Working status: Employed"))
        print("Working status: Employed")


    elif User1_working_status=="2":
        f.write("{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n".format("Name: " + user1_title + " " + user1_name, "Gender:"
        + user1_gender,"Town and country: " + user1_town, "Age: " + user1_age,"Country and town of birth: "
        + user1_country_of_birth,"Nationality: "+ user1_nationality,
        "Country residence:"+user1_country_residence,"Tax resident country: "+ user1_tax_res,
        "Working status: Self Employed"))
        print("Working status: Self Employed")

Is there a way to shorten this? I have the same for user 2 but then I have to do everything again with user2_working_status and the code gets too long since I have 9 options..So if there's a way to combine both user1 and user2 in this code?:)

bvl92
  • 31
  • 5
  • yes there is. can you list all the differences between the 2 cases please? – Aprillion Jan 06 '19 at 11:00
  • 1
    It's the same for user 2. I just switch User1_working_status to User2_working_status and user1_title to user2_title and so on:) – bvl92 Jan 06 '19 at 11:04
  • If you write line by line and use `.format()` for one line it will get much more readable. – Klaus D. Jan 06 '19 at 11:31

2 Answers2

1

The long format string is also quite unreadable. How about this?

with open("bankaccount.txt", 'a+') as f:
    if User1_working_status in ("1", "2"):
        working_status_label = "Employed" if User1_working_status == "1" else "Self Employed"
        for label, value in [
                ("Name", user1_title),
                ("Gender", user1_gender),
                ("Town and country", user_1_town),
                ("Age", user1_age),
                ("Country and town of birth",  user1_country_of_birth),
                ("Nationality", user1_nationality),
                ("Country residence", user1_country_residence),
                ("Tax resident country", user1_tax_res),
                ("Working status", working_status_label)]:
            f.write("{0}: {1}\n".format(label, value))
        print("Working status: {0}".format(working_status_label)
tripleee
  • 175,061
  • 34
  • 275
  • 318
0

You can create a dictionary with the labels for each status value like this:

status_labels = {
    "1": "Employed",
    "2": "Self Employed"
}

with open("bankaccount.txt", 'a+') as f:
    ...
    print("Working status: {}".format(status_labels[User1_working_status]))

edit: and for multiple users, it would be best to iterate over a list of dicts and using str.format(**dict) as per How do I format a string using a dictionary in python-3.x?, e.g. like this:

users = [{"title": "dd", "working_status": ...}, ...]

with open("bankaccount.txt", 'a+') as f:
    for user in users:
        ..."Name: {title}, Working status: {working_status}".format(**user)
Aprillion
  • 21,510
  • 5
  • 55
  • 89
  • I was going to suggest the same. I would also suggest that you read about [string formatting](https://docs.python.org/3.4/library/string.html#format-string-syntax), which will enormously improve the readability and simplify your code – buran Jan 06 '19 at 11:14
  • updated with a suggestion about multiple users, hope it helps – Aprillion Jan 06 '19 at 11:16
  • @buran missing keys in the dict cause exception on purpose, if you want to suggest `status_labels.get(User1_working_status, "Unknown")`, please suggest so without editing my answer, thanks! – Aprillion Jan 06 '19 at 11:35