aquatic7
aquatic7

Reputation: 635

How do I make these two input() in a loop to avoid unecessary many input() lines

I want to input how many victims, and then be able to input name and age for each victims.

But I can't seem to wrap my head around how to make a loop that ask for input for each victim. Right now I make one input and the same input is printed x times.

I can see the logic in how it works now, but I simply can't figure out how to do it without making 5 x separate input for "name" and 5 x separate input for "age".

num_victims = input("How many victims: ")

inc_numbr = 1
v_name = input(str(inc_numbr) + "." + " Victims " + "name: ")
v_age = input(str(inc_numbr) + "." + " Victims " + "age: ")
inc_numbr += 1

v_numbr = 1
v_one = (f"__Victim:__ \n"
         f"Name: {v_name}\n"
         f"Estimated age: {v_age} years old.\n\n")
v_numbr += 1
v_two = (f"__Victim " + str(v_numbr) + ":__\n"
         f"Name: {v_name}\n"
         f"Estimated age: {v_age} years old.\n\n")
v_numbr += 1
v_three = (f"__Victim " + str(v_numbr) + ":__\n"
           f"Name: {v_name}\n"
           f"Estimated age: {v_age} years old.\n\n")
v_numbr += 1
v_four = (f"__Victim " + str(v_numbr) + ":__\n"
          f"Name: {v_name}\n"
          f"Estimated age: {v_age} years old.\n\n")
v_numbr += 1
v_five = (f"__Victim " + str(v_numbr) + ":__\n"
          f"Name: {v_name}\n"
          f"Estimated age: {v_age} years old.\n")
v_numbr += 1

Added for output format example:

__Victim:__

Name: xxxxx

Age: xxxx

__Victim 2:__

Name: xxxxx

Age: xxxx

Ect..

Upvotes: 0

Views: 69

Answers (2)

Ari Cooper-Davis
Ari Cooper-Davis

Reputation: 3485

You need to wrap it up in a for loop:

number_of_victims = int(input("How many victims? "))
victim_names = []
victim_ages = []
for i in range(1, number_of_victims+1):
    victim_names.append(input("Name of victim "+str(i)+": "))
    victim_ages.append(input("Age of victim "+str(i)+": "))
print(victim_names, victim_ages)

Note how I have created the victim_names and victim_ages objects outside of the for loop so that they're not overwritten. These objects are lists - they're empty initially, but we append data to them inside the for loop.

You could use a dictionary object for victims instead if you wanted, but as Alex F points out below, if you have two victims with the same name this may result in data being overwritten:

number_of_victims = int(input("How many victims? "))
victims = dict()
for i in range(1, number_of_victims+1):
    name = input("Name of victim "+str(i)+": ")
    age = input("Age of victim "+str(i)+": ")
    victims[name] = int(age)
print(victims)

In the future, when approaching a problem like this, you might find it helpful to write pseudo-code. This is like a plan for what your code will look like without having to worry about the syntax and rules for actually writing it. For example some pseudo-code for this problem might look like:

  • ask for the number of victims
  • make an empty object to store the victim information
  • repeat this as many times as there are victims:
    • ask for the name of the victim
    • ask for the age of the victim
    • add victim information into the object
  • display the contents of the object

In answer to the question in the comments, how to format the printing, you could do something like this:

for v in range(number_of_victims):
    print("Victim ",str(v+1),":")
    print("Name:",victim_names[v])
    print("Age:",victim_ages[v])

Upvotes: 1

Alex F
Alex F

Reputation: 456

Loop is used for code that needs to be run multiple times, therefore if you need to ask for number of victims once, but then enter multiple name's and age's, you can do it like this:

number_of_victims = int(input("Enter the number of victims: "))
victims = []
for i in range(number_of_victims):
    name = input("Victim #{} name: ".format(i+1))
    age = input("Victim #{} age: ".format(i+1))
    victims.append([name, age])

This way you get a list, where each element is a list of name and age. Now you can access file number i with victims[i-1] (because list indices start with 0).

Another option would be using a dictionary:

number_of_victimes = int(input("..."))
victims = {}
for i in range(number_of_victims):
    name = input("...")
    age = input("...")
    victims[i] = [name, age]

You can access file number i with victims[i-1] (because range starts at 0 as well) but you can change this if you write for i in range(1, int(number_of_victims)+1):. Now each dictionary key is the "human" index (starting from 1), instead of "computer's" index (starting from 0), so you can access the same file number i using victims[i].

Alternatively, you could make a dictionary where each key is the name of the victim, but in case you ever have 2 identical names, the previous key-value pair you have added using that name will get overwritten:

number_of_victimes = int(input("..."))
victims = {}
for i in range(number_of_victims):
    name = input("...")
    age = input("...")
    victims[name] = int(age)

Upvotes: 1

Related Questions