Reputation: 31
i'm new to programming, and i need your help on my problem.
i made a code, a loop input like this:
for x in range (2):
name = input('name : ')
age = input('age : ')
print()
print('name', name)
print('age', age)
print('---------------')
the output is like this.
name : garox
age : 29
name garox
age 29
---------------
name : supri
age : 16
name supri
age 16
---------------
i want the output to be separated from each inputs, like this:
--- input ---
name : garox
age : 29
name : supri
age : 16
--- output ----
name garox
age 29
name supri
age 16
so i want to input all data first, then output comes after i finished all inputs.
also using loop, not doing 'print' one by one. is it possible? if so, how should i do that?
thanks in advance.
thanks for your answers ! that really helps me to understand it better.
i tried to put it inside a class. but i got this error:
Dataa.looop(self)
NameError: name 'self' is not defined
here's how i arrange the code.
class Dataa():
def __init__(self, name, age):
self.name = name
self.age = age
def looop (self):
list = []
for x in range(2):
name = input('name : ')
age = input('age : ')
list.append({'name': name, 'age': age})
for input in list:
print('name', input['name'])
print('age', input['age'])
print('---------------')
Dataa.looop(self)
how do i call the loop def, so i can make the inputs?
Upvotes: 2
Views: 669
Reputation: 2299
You can create a list that will store each input as a dictionary with two keys: name and age.
Then you create two for
loops : The first one to store the inputs and the second one to print data. I think this approach is better because you can use it to add as many as you want.
persons = []
for x in range (2):
name = input('name : ')
age = input('age : ')
country = input('country : ')
persons.append({'name': name, 'age': age, 'country': country})
for input in persons:
print('name', input['name'])
print('age', input['age'])
print('country', input['country'])
print('---------------')
OUTPUT :
name : john
age : 27
name : david
age : 17
name john
age 27
----------------
name david
age 17
----------------
Upvotes: 0
Reputation: 446
Try this with adding input strings to a list:
persons=[]
print("-----input------")
for x in range (2):
name = input('name : ')
age = input('age : ')
persons.append([name,age])
print('--------output-------')
for x in persons:
print("name:",x[0])
print("age:", x[1])
Result:
-----input------
name : name1
age : 30
name : name2
age : 30
--------output-------
name: name1
age: 30
name: name2
age: 30
Upvotes: 2