0

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..

aquatic7
  • 615
  • 1
  • 6
  • 19
  • This is probably of interest for you: https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – Mr. T Jan 11 '18 at 17:56

2 Answers2

1

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])
Ari Cooper-Davis
  • 3,374
  • 3
  • 26
  • 43
  • I haven't really worked with list and dictionaries before. I am going to write it to a text file. That I know how to do :) But I would like to be able to pull each result and format it to look something like this: `__Victim:__ Name: xxxxx Age: xxxx __Victim 2:__ Name: xxxxx Age: xxxx Ect... ` Can you help me or give me a hint on how to do this? Thank you very much for your help. – aquatic7 Jan 11 '18 at 18:51
  • I can't tell you how to format those without using a list or a dictionary I'm afraid, unless you did the `print`ing within the same `for` loop that you collected the `input` in... It's well worth [learning how to use lists](http://introtopython.org/lists_tuples.html), they're really useful and you won't be able to get very far without them! – Ari Cooper-Davis Jan 11 '18 at 18:55
  • I will use the lists and read the link you just gave me to learn about this. I just wanted to hear if you could tell me if I should do something like `target.write("__Victim 1:__ \n" + "Name: " + victim_names[0] + "\nAge: " + victim_ages[0])` And write that multiple times for each victim number I want written to the file. – aquatic7 Jan 11 '18 at 19:09
  • 1
    Worked perfect!! and now I have a start understanding of how lists and for loops works. I can read up on those and use this as a starting point to play around with it and get comfortable using it. Question closed, thanks for your help! – aquatic7 Jan 11 '18 at 19:35
1

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)
Alex F
  • 394
  • 1
  • 12