Use input variable multiple times with other variables included

Hey, im very new to python(just 2 Weeks). I write a new programm where a user typer can watch his contact informations and edit them (i learn Lists at the moment)
Here is the code (dont kill me its not finished but i want to fix the problem than i do the rest)

end = " "
l = ["Anton", "Lea", "Josef"]
second_name = ["Himalaya", "Bertel","Stalin"]
age = [23, 40, 102]
counter = 0
change_input = ["Pease Type in the new First Name", "Pease Type in the new First Name", "Please Type in the new Age"]
change_out = ["was successfully changed"]


option = " "
while option != 3:
    x = ["1 == Show Contacts\n", "2 == Add Contact Information\n", "3 == edit Contact Informations\n"]
    print(x)
    option = input("please choose an option:\n")
    if int(option) == 1 or int(option) == 2 or int(option) == 3:
        print(l)
        show_contact = int(input("Which Contact Informations do you want to look like?\n"))

        if show_contact == 1:
            print(l[0], second_name[0], age[0], " Years old")
        elif show_contact == 2:
            print(l[1], second_name[1], age[1], " Years old")
        elif show_contact == 3:
            print(l[2], second_name[2], age[2], " Years old")
        edit = int(input("Do you want to edit the contact informations?\nPress 1 for yes 2 for no\n"))
        if edit == 1:
            what = ["1 == First Name\n", "2 == Last Name\n", "3 == Age\n"]
            print(what)
            edit_what = int(input("Which Informations do you want to edit?"))
            if edit_what == 1:
                l[0] = input("Pease Type in the new First Name")
                print("Name successfully changed")
                input("please confirm with press any button")
            elif edit_what == 2:
                second_name[1] = input("Pease Type in the new Last Name")
                print("Name successfully changed")
                input("please confirm with press any button")

            elif edit_what == 3:
                age[1] = int(input("Please Type in the new Age"))
                print("Age successfully changed")
                input("please confirm with press any button")
            else:
                print("Dont understand. Please try again")

> Blockquote

# Codeblock for later


while end != 1:
    a = input("Please type in the name of the Person that you want to add to the list\n")
    l.append(a)
    sn = input("type in the last name")
    second_name.append(sn)
    b = int(input("How old is the Person?\n"))
    age.append(b)
    print("You got", (l[-1]), second_name[-1], "at the age of", age[-1], "successfully addet.\n "
                                                                         "Changes are saved!")
    print(l)

    end = input("to close press 1 for continue press 2")
    if int(end) == 1:
        print("closed")
        break
    else:
        continue

So the Problem is: For example he choose at show_contact number 1.
Than i have to type all options for every contact but
I dont want to type in the same 3 Times
Is there a way just type it in 1 time and then put it in if i need it? Hope you understand. If not just ask me.

You’re only ever indexing with a literal number. Try using expressions.

For example, instead of:

        if show_contact == 1:
            print(l[0], second_name[0], age[0], " Years old")
        elif show_contact == 2:
            print(l[1], second_name[1], age[1], " Years old")
        elif show_contact == 3:
            print(l[2], second_name[2], age[2], " Years old")

you can do:

        if show_contact >= 1 and show_contact <= 3:
            print(l[show_contact - 1], second_name[show_contact - 1], age[show_contact - 1], " Years old")
        else:
            print("No such contact")
            continue

Incidentally, it would be clearer if you renamed the list l to first_name.

Also, as you’re going to add new contacts (and maybe also delete contacts), it would be a good idea to count the numbers of contacts using len(first_name) instead of having the fixed number 3.

1 Like

[…] (i learn Lists at the moment)
Here is the code (dont kill me its not finished but i want to fix the problem than i do the rest)

end = " "
l = ["Anton", "Lea", "Josef"]
second_name = ["Himalaya", "Bertel","Stalin"]
age = [23, 40, 102]

Ok, so you start with 3 people.
I’d be calling l the name first_name; it matches second_name and
is just generally more information.

[…]

So the Problem is: For example he choose at show_contact number 1.
Than i have to type all options for every contact but
I dont want to type in the same 3 Times
Is there a way just type it in 1 time and then put it in if i need it? Hope you understand. If not just ask me.

I am guessing that you’re referring to these lines:

option = " "
while option != 3:
x = [“1 == Show Contacts\n”, “2 == Add Contact Information\n”, “3 == edit Contact Informations\n”]
print(x)

   option = input("please choose an option:\n")
   if int(option) == 1 or int(option) == 2 or int(option) == 3:
       print(l)
       show_contact = int(input("Which Contact Informations do you want to look like?\n"))
       if show_contact == 1:
           print(l[0], second_name[0], age[0], " Years old")
       elif show_contact == 2:
           print(l[1], second_name[1], age[1], " Years old")
       elif show_contact == 3:
           print(l[2], second_name[2], age[2], " Years old")

One of the big things with lists is that you can index them. Noting that
people count from 1 but most computer indices count from 0, you
could write:

 contact_index = show_contact - 1
 print(l[contact_index], second_name[contact_index], age[contact_index], " Years old")

which will print whichever person the user chose.

   edit = int(input("Do you want to edit the contact informations?\nPress 1 for yes 2 for no\n"))
   if edit == 1:
       what = ["1 == First Name\n", "2 == Last Name\n", "3 == Age\n"]
       print(what)

Here you’re choosing what aspect of a person to edit. I’m sure you can
imagine that if we get more attributes (city, vocation, etc) this will
get verbose quite quickly.

You could change how you represent people. Suppose you decided to keep
all a person’s attributes together in a single list like:

 ["Anton", "Himalaya", 24]

You could write the initial list of people as:

 people = [
     ["Anton", "Himalaya", 23],
     ["Lea", "Bertel", 40],
     ["Josef", "Stalin", 102],
 ]

which is a single list containing 3 elements, each of which is a list.

The advantage here is that you can choose which part of a person to edit
with an index. A hardwired example:

 lea_index = 1
 second_name_index = 1
 people[lea_index][second_name_index] = "Bertel New"

Remembering that indices count from 0:

  • the expression people is a reference to your list of people.
  • the expression people[lea_index] is a reference to Lea Bertel.
  • the expression people[lea_index][second_name_index] is a reference
    to the “second name” element of Lea Bertel.

So when you ask for the value of edit_what:

 edit_what = int(input("Which Informations do you want to edit?"))

you can then compute:

 what_index = edit_what - 1

and then modify it directly:

 people[contact_index][what_index] = new_value

where new_value comes from your next question.

You can even keep an index of the field names:

 fields = ["First Name", "Last Name", "Age"]

Then you can offer the user choices:

 print("What to edit?")
 for i, field in enumerate(fields):
     print(i+1, field)
 edit_what = int(input("Which Informations do you want to edit?"))

and so on and so on.

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

Wow thank you very much, you helped me a lot!